SpringBoot multi-threaded execution task task

I. Description of the problem

  Task scheduled task default is single-threaded execution, scheduled tasks if there are a lot of words, it may cause a lot of tasks can not be on time and accurate execution, for example:

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskTest {
    private final Logger log = LoggerFactory.getLogger(TaskTest02.class);
    //输出时间格式
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:sss");

    @Scheduled(cron = "0/15 * * * * ? ")
    private void sayHello(){
        String dateTime = format.format(new Date());
        log.info("{} 向宇宙发出了一声问候: Hello World!", dateTime);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0/16 * * * * ? ")
    private void sayHello2(){
        String dateTime = format.format(new Date());
        log.info("{} 向宇宙发出了一声问候: 你好,世界", dateTime);
    }
}

When sayHello () method of execution, because prolonged occupation of task execution thread, resulting in sayHello2 () was forced to delay the implementation of backward, as shown:

SpringBoot multi-threaded execution task task

II. Solution

plan 1

Add the following block of code can be placed in any of a class, the whole project can simply add a

@Bean
public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    // 设置scheduler执行线程为3个
    scheduler.setPoolSize(3);
    return scheduler;
}

SpringBoot multi-threaded execution task task

Scenario 2 (personal recommendation)

You can add a configuration class, timed task class or method without any change

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @描述: 多线程执行定时任务
 * @日期 2019年5月28日
 */
@Configuration
public class TaskConfig {
    /**
     * @描述: 所有的定时任务都放在一个线程池中,定时任务启动时使用不同的线程
     * @return
     * @日期 2019年5月28日
     */
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 设置scheduler执行线程为3个
        scheduler.setPoolSize(3);
        return scheduler;
    }
}
Scenario 3

Class can add a configuration (SchedulingConfigurer implement the interface), the timing task class or method without any change

import java.util.concurrent.Executors;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @描述: 多线程执行定时任务
 * @日期 2019年5月27日
 */
@Configuration
public class SchedulingConfig implements SchedulingConfigurer {
    /**
     * @描述: 所有的定时任务都放在一个线程池中,定时任务启动时使用不同的线程
     * @param taskRegistrar
     * @日期 2019年5月27日
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //设定一个定时任务线程池,数量为3
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(3));
    }
}

III. Implementation of the results

We can see two separately timed task has been performed by different threads

SpringBoot multi-threaded execution task task

Guess you like

Origin blog.51cto.com/1197822/2401581