java 线程 spring_java中spring里实现多线程
Spring通過任務執行器(TaskExecutor)來實現多線程和并發編程的
可使用ThreadPoolTaskExecutor來實現基于線程池的TaskExecutor
在實際開發中由于多是異步,所以使用@EnableAsync來支持異步任務,且要在Bean的方法中使用@Async來聲明其是一個異步任務
????? 以下實例:
??? 配置類
class="配置類TaskExecutorConfig" name="code">package com.zgw.taskexecutor;
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;
@Configuration
@ComponentScan("com.zgw.taskexecutor")
@EnableAsync //開啟對異步任務的支持
public class TaskExecutorConfig implements AsyncConfigurer {
/**
* 通過實現AsyncConfigurer接口,重寫getAsyncExecutor()方法,
* 返回一個ThreadPoolTaskExecutor對象,這樣實現一個基于線程池
* TaskExecutor
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
任務執行類
?????
Service" name="code">package com.zgw.taskexecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncTaskService {
@Async //聲明是一個異步方法
public void executeAsyncTaskOne(int i){
System.out.println("執行異步任務: "+i);
}
@Async
public void executeAsyncTaskTwo(int i){
System.out.println("執行異步任務加1操作:"+(i+1));
}
}
?
?
3.運行
package com.zgw.taskexecutor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestExecutor {
public static void main(String[] args) {
//使用AnnotationConfigApplicationContext作為spring容器,
//接收輸入一個配置類作為參數
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
//獲得聲明配置的AsyncTaskService的Bean
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
for(int i =0 ;i<20;i++){
asyncTaskService.executeAsyncTaskOne(i);
asyncTaskService.executeAsyncTaskTwo(i);;
}
context.close();
}
}
?
3.運行
?
package com.zgw.taskexecutor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestExecutor {
public static void main(String[] args) {
//使用AnnotationConfigApplicationContext作為spring容器,
//接收輸入一個配置類作為參數
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
//獲得聲明配置的AsyncTaskService的Bean
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
for(int i =0 ;i<20;i++){
asyncTaskService.executeAsyncTaskOne(i);
asyncTaskService.executeAsyncTaskTwo(i);;
}
context.close();
}
}
?
? 運行結果如下:
?
?
?結果是并發執行而不是順序執行的。
?
????
spring_thread.rar (10.1 KB)
下載次數: 0
總結
以上是生活随笔為你收集整理的java 线程 spring_java中spring里实现多线程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爬虫python能做游戏吗_一入爬虫深似
- 下一篇: 日照职业单招计算机专业,日照职业技术学院