Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 TaskScheduler performance issue

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(Runnab‌​le 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,

  1. Is my thinking correct that jvm creates new daemon thread on each scheduler.schedule() call (Rather than creating thread at the date-time specified) ?

  2. 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 ?

  3. Else, will it be a performance issue ?

like image 703
Ramanujan R Avatar asked Oct 30 '22 19:10

Ramanujan R


1 Answers

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

like image 136
kuhajeyan Avatar answered Nov 15 '22 09:11

kuhajeyan