本篇主要描述一下spring的多線程的使用與定時任務的使用.
1.spring多線程任務的使用
spring通過任務執行器TaskExecutor來實現多線程與並發編程。通常使用ThreadPoolTaskExecutor來實現一個基於線程池的TaskExecutor.
首先你要實現AsyncConfigurer 這個接口,目的是開啟一個線程池
代碼如下:
package com.foreveross.service.weixin.test.thread; import java.util.concurrent.Executor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; /** * 注入一個線程池 * @author mingge * */ @Configuration @ComponentScan("com.foreveross.service.weixin.test.thread") @EnableAsync public class TaskExecutorConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(20); taskExecutor.setQueueCapacity(25); taskExecutor.initialize(); return taskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; } }
然后注入一個類,實現你的業務,並在你的Bean的方法中使用@Async注解來聲明其是一個異步任務
代碼如下:
package com.foreveross.service.weixin.test.thread; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** * 線程池任務 * @author mingge * */ @Service public class TaskService { @Async public void executeAsyncTask(int i){ System.out.println("執行異步任務:"+i); } @Async public void executeAsyncTask1(int i){ System.out.println("執行異步任務1:"+(i+i)); } }
最后通過測試,可以看到你的實現是異步執行了.
package com.foreveross.service.weixin.test.thread; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * * @author mingge * */ public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskExecutorConfig.class); TaskService taskService=context.getBean(TaskService.class); for(int i=0;i<20;i++){ taskService.executeAsyncTask(i); taskService.executeAsyncTask1(i); } //最后可以根據結果可以看出結果是並發執行而不是順序執行的呢 context.close(); } }
1.spring定時任務的使用
在java原生態中,我們使用timer就可以了,這里小編說一些在Spring中的定時任務的使用
package com.foreveross.service.weixin.test.thread; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @ComponentScan("com.foreveross.service.weixin.test.thread") @EnableScheduling//開啟對定時器的支持 public class TaskSchedulerConfig { }
package com.foreveross.service.weixin.test.thread; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class TimerTaskJob { @Scheduled(fixedRate=2000) public void test(){ System.out.println("我是定時任務:"+new Date().getSeconds()); } }
package com.foreveross.service.weixin.test.thread; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestTimer { public static void main(String[] args) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskSchedulerConfig.class); //context.close(); } }
記一下總結,或許以后有用...
每天進步一點點...
