• JDK 线程池
- ExcecutorService
- ScheduledExecutorService
案例代码:
1. private void sleep(long m) { 2. try { 3. Thread.sleep(m); 4. } catch (InterruptedException e) { 5. e.printStackTrace(); 6. } 7. } 8. 9. // 1.JDK普通线程池 10. @Test 11. public void testExecutorService() { 12. Runnable task = new Runnable() { 13. @Override 14. public void run() { 15. logger.debug("Hello ExecutorService"); 16. } 17. }; 18. 19. for (int i = 0; i < 10; i++) { 20. executorService.submit(task); 21. } 22. 23. sleep(10000); 24. } 25. 26. // 2.JDK定时任务线程池 27. @Test 28. public void testScheduledExecutorService() { 29. Runnable task = new Runnable() { 30. @Override 31. public void run() { 32. logger.debug("Hello ScheduledExecutorService"); 33. } 34. }; 35. 36. scheduledExecutorService.scheduleAtFixedRate(task, 10000, 1000, TimeUnit.MILLISECONDS); 37. 38. sleep(30000); 39. }
• Spring 线程池
- ThreadPoolTaskExecutor
- ThreadPoolTaskScheduler
首先在配置文件中加入相关配置:
普通任务的线程池配置
1. # TaskExecutionProperties 2. spring.task.execution.pool.core-size=5 3. spring.task.execution.pool.max-size=15 4. spring.task.execution.pool.queue-capacity=100
定时任务的线程池配置
spring.task.scheduling.pool.size=5
增加spring线程池配置类
1. package com.nowcoder.community.config; 2. 3. import org.springframework.context.annotation.Configuration; 4. import org.springframework.scheduling.annotation.EnableAsync; 5. import org.springframework.scheduling.annotation.EnableScheduling; 6. 7. @Configuration 8. @EnableScheduling 9. @EnableAsync 10. public class ThreadPoolConfig { 11. }
编写测试程序
1. // 3.Spring普通线程池 2. @Test 3. public void testThreadPoolTaskExecutor() { 4. Runnable task = new Runnable() { 5. @Override 6. public void run() { 7. logger.debug("Hello ThreadPoolTaskExecutor"); 8. } 9. }; 10. 11. for (int i = 0; i < 10; i++) { 12. taskExecutor.submit(task); 13. } 14. 15. sleep(10000); 16. } 17. 18. // 4.Spring定时任务线程池 19. @Test 20. public void testThreadPoolTaskScheduler() { 21. Runnable task = new Runnable() { 22. @Override 23. public void run() { 24. logger.debug("Hello ThreadPoolTaskScheduler"); 25. } 26. }; 27. 28. Date startTime = new Date(System.currentTimeMillis() + 10000); 29. taskScheduler.scheduleAtFixedRate(task, startTime, 1000); 30. 31. sleep(30000); 32. }
• 分布式定时任务
- Spring Quartz
首先引入Quartz依赖
1. <dependency> 2. <groupId>org.springframework.boot</groupId> 3. <artifactId>spring-boot-starter-quartz</artifactId> 4. </dependency>
重写job下面的execute方法
1. package com.nowcoder.community.quartz; 2. 3. import org.quartz.Job; 4. import org.quartz.JobExecutionContext; 5. import org.quartz.JobExecutionException; 6. 7. public class AlphaJob implements Job { 8. @Override 9. public void execute(JobExecutionContext context) throws JobExecutionException { 10. System.out.println(Thread.currentThread().getName() + ": execute a quartz job."); 11. } 12. }
增加quartz线程池配置类
1. package com.nowcoder.community.config; 2. 3. import com.nowcoder.community.quartz.AlphaJob; 4. import com.nowcoder.community.quartz.PostScoreRefreshJob; 5. import org.quartz.JobDataMap; 6. import org.quartz.JobDetail; 7. import org.springframework.context.annotation.Bean; 8. import org.springframework.context.annotation.Configuration; 9. import org.springframework.scheduling.quartz.JobDetailFactoryBean; 10. import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; 11. 12. // 配置 -> 数据库 -> 调用 13. @Configuration 14. public class QuartzConfig { 15. 16. // FactoryBean可简化Bean的实例化过程: 17. // 1.通过FactoryBean封装Bean的实例化过程. 18. // 2.将FactoryBean装配到Spring容器里. 19. // 3.将FactoryBean注入给其他的Bean. 20. // 4.该Bean得到的是FactoryBean所管理的对象实例. 21. 22. // 配置JobDetail 23. // @Bean 24. public JobDetailFactoryBean alphaJobDetail() { 25. JobDetailFactoryBean factoryBean = new JobDetailFactoryBean(); 26. factoryBean.setJobClass(AlphaJob.class); 27. factoryBean.setName("alphaJob"); 28. factoryBean.setGroup("alphaJobGroup"); 29. factoryBean.setDurability(true); 30. factoryBean.setRequestsRecovery(true); 31. return factoryBean; 32. } 33. 34. // 配置Trigger(SimpleTriggerFactoryBean, CronTriggerFactoryBean) 35. // @Bean 36. public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) { 37. SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean(); 38. factoryBean.setJobDetail(alphaJobDetail); 39. factoryBean.setName("alphaTrigger"); 40. factoryBean.setGroup("alphaTriggerGroup"); 41. factoryBean.setRepeatInterval(3000); 42. factoryBean.setJobDataMap(new JobDataMap()); 43. return factoryBean; 44. } 45. 46. 47. 48. }
配置quartz
1. # QuartzProperties 2. spring.quartz.job-store-type=jdbc 3. spring.quartz.scheduler-name=communityScheduler 4. spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO 5. spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX 6. spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate 7. spring.quartz.properties.org.quartz.jobStore.isClustered=true 8. spring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool 9. spring.quartz.properties.org.quartz.threadPool.threadCount=5
此时只要启动项目就会输出
: execute a quartz job.