SpringBoot添加定時任務入門請看這裏=>SpringBoot入門八,添加定時任務java
Task定時任務默認都是使用單線程執行的,若是定時任務有不少的話,那麼可能會致使不少任務沒法按時準確執行,示例以下:spring
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); } }
當sayHello()方法執行的時候,由於長時間佔用任務執行線程,致使sayHello2()被迫向後延時執行,如圖:多線程
添加如下代碼塊,可放置在任意一個類中,整個工程只須要添加一個便可ide
@Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); // 設置scheduler執行線程爲3個 scheduler.setPoolSize(3); return scheduler; }
添加一個配置類便可,定時任務類或方法不用作任何改變線程
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; } }
添加一個配置類便可(實現SchedulingConfigurer接口),定時任務類或方法不用作任何改變code
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)); } }
能夠看到兩個定時任務已經分別由不一樣的線程執行了orm