java使⽤@Async注解+线程池异步处理数据-主线程等待⼦线程返回处理数据
1 在@SpringBootApplication启动类 添加注解@EnableAsync
2 异步⽅法使⽤注解@Async ,返回值为void或者Future
3 切记⼀点 ,异步⽅法和调⽤⽅法⼀定要**** 写在不同的类中 ****,如果写在⼀个类中,
是没有效果的
fig;
import org.springframework.beans.factory.annotation.Qualifier;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.urrent.ThreadPoolTaskExecutor;
import urrent.Executor;
/**
* 线程池配置
*
* @author YJX
* @date 2019/10/12 15:17
*/
@Configuration
@EnableAsync
public class ExecutorConfig {
/
/@Autowired
//private TraceableThreadFactory traceableThreadFactory;
await和async使用方法/**
* 默认的线程池
*
* @return
*/
@Bean
@Primary
@Qualifier("defaultExecutor")
public Executor defaultExecutor(){
ThreadPoolTaskExecutor executor =new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);/*核⼼线程数*/
executor.setMaxPoolSize(10);/*最⼤线程数*/
executor.setQueueCapacity(10000);/*队列⼤⼩*/
executor.setKeepAliveSeconds(60);/* 某线程空闲超过1分钟,就回收该线程*/
//executor.setAllowCoreThreadTimeOut(true); // KeepAliveSeconds 设置也作⽤于【核⼼线程数】
executor.setThreadNamePrefix("defaultExecutor-");
//executor.setThreadFactory(traceableThreadFactory);
//executor.setAwaitTerminationSeconds(3);
executor.setRejectedExecutionHandler(null);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
return executor;
}
/**
* 线程池
*
* @return
*/
@Bean
@Qualifier("executor")
public Executor reconciliationExecutor(){
ThreadPoolTaskExecutor executor =new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);/*核⼼线程数*/
executor.setMaxPoolSize(3);/*最⼤线程数*/
executor.setMaxPoolSize(3);/*最⼤线程数*/
executor.setQueueCapacity(10000);/*队列⼤⼩*/
executor.setKeepAliveSeconds(60*3);/* 某线程空闲超过3分钟,就回收该线程*/
executor.setAllowCoreThreadTimeOut(true);// KeepAliveSeconds 设置也作⽤于【核⼼线程数】 executor.setThreadNamePrefix("executor-");
//executor.setThreadFactory(traceableThreadFactory);
executor.setRejectedExecutionHandler(null);
executor.setWaitForTasksToCompleteOnShutdown(true);
//executor.setAwaitTerminationSeconds(3);
executor.initialize();
return executor;
}
}
@GetMapping("thread")
public void store(){
long startTime = System.currentTimeMillis();
ArrayList<Object> list =new ArrayList<>();
try{
System.out.println("主线程开始");
Future<String> r1 = asyncThread.doTask1();
Future<String> r2 = asyncThread.doTask2();
while(true){
System.out.println("主线程等待中");
if(r1.isDone()&& r2.isDone()){
list.());
list.());
break;
}else{
TimeUnit.SECONDS.sleep(1);
}
}
}catch(Exception e){
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("总耗时 = "+(endTime - startTime));
System.out.println("list = "+ list);
}
注意!
@Async异步注解要单独写⼀个类, 不能写在调⽤者的类⾥⾯ , 否则是单线程执⾏
3 切记⼀点 ,异步⽅法和调⽤⽅法⼀定要**** 写在不同的类中 ****,如果写在⼀个类中,
是没有效果的
就是下⾯的代码不能和调⽤者写在同⼀个类⾥⾯
/*
* Copyright 2019 Wicrenet, Inc. All rights reserved.
*/
ller;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import urrent.Future;
import urrent.TimeUnit;
/**
* 【】
*
* @author YJX
* Created on 2019/10/12 15:08
*/
@Component
public class AsyncThread {
@Async("executor")
public Future<String>doTask1(){
System.out.println("线程1开始");
long l = System.currentTimeMillis();
try{
TimeUnit.SECONDS.sleep(3);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("t1结束耗时 = "+(System.currentTimeMillis()- l));
return new AsyncResult<>("d1");
}
@Async("executor")
public Future<String>doTask2(){
System.out.println("线程2开始");
long l = System.currentTimeMillis();
try{
TimeUnit.SECONDS.sleep(3);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("t2结束耗时 = "+(System.currentTimeMillis()- l));
return new AsyncResult<>("d2");
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论