I am building a Spring 4 Rest API for a trade automation site.
In service layer a cronjob is dynamically created using Spring TaskScheduler.schedule(Runnable arg0, Date arg1)
interface which will create a Runnable
, to be executed at the time given as parameter, exactly once. This thread will call another service to access my Hibernate DAO layer and do something in future.
The configuration class and implementation is given below.
@Configuration
@EnableWebMvc
@EnableScheduling
@ComponentScan("com.example")
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public ThreadPoolTaskScheduler taskScheduler() {
return new ThreadPoolTaskScheduler();
}
}
@Service
public class TransactionServiceImp implements TransactionService {
@Autowired
private TaskScheduler scheduler; //org.springframework.scheduling.TaskScheduler;
@Autowired
private TaskExecutorService taskService; //My service
@Transactional
public myFunction(){
//some code
final Long key = //some id value from db
Date exeTime = //some java.util.Date in future
Runnable runnable = new Runnable() {
private long id = key;
public void run() {
taskService.doSomething(id);
}
};
ScheduledFuture<?> sheduler = scheduler.schedule(runnable, exeTime);
//some code
}
}
This code is working fine and executes the task at exact time and exactly once.
By means of eclipse debugging, its found that a new daemon thread is created each time scheduler.schedule()
is called (when processing http requests to spring mvc). My problem is,
Is my thinking correct that jvm creates new daemon thread on each
scheduler.schedule()
call (Rather than creating thread at the date-time specified) ?The created daemon threads are still showing
Running
status in Eclipse debug even after the task executed. Will the thread be destroyed or not on finishingrun()
method ?Else, will it be a performance issue ?
ThreadPoolTaskScheduler wrapper around ScheduledThreadPoolExecutor(an underlying jdk implementation), threads can be reused from predefined set of worker threads. Each tasks are assigned/processed to work queue.
even core threads are initially created and started only when new tasks arrive
Is my thinking correct that jvm creates new daemon thread on each scheduler.schedule() call (Rather than creating thread at the date-time specified) ?
Your are creating thread instance(Runnable) each time, since it accepts runnable, hence your task should be executed or existing worker thread in the queue which may be idle. if max pool size is reached your task will wait.
The created daemon threads are still showing Running status in Eclipse debug even after the task executed. Will the thread be destroyed or not on finishing run() method ?
Java doc says this,
Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.
Else, will it be a performance issue ? Maximum optimal number of thread that can be run are limited by cpu cores. So increasing pool size does not, necessarily increase performance
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With