java5以后,线程有了很大的变化,在使用上更加方便功能更佳强大,Springboot里面进行了进一步的封装。
我们来看一个例子
package com.executor;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、做网站、乐东黎族网络推广、微信小程序、乐东黎族网络营销、乐东黎族企业策划、乐东黎族品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供乐东黎族建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
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@EnableAsync
br/>@EnableAsync
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(15);
executor.setThreadNamePrefix("Anno-Executor");
executor.setQueueCapacity(25);
executor.initialize();
System.out.println("initialize complete ..");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// .....
System.out.println("do somethings by myself ...");
}
});
// 使用预定义的异常处理类
// executor.setRejectedExecutionHandler(new
// ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
package com.executor;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;@Service
public class SyncService {@Async
br/>@Async
System.out.println("进入service。。。");
try {
Thread.sleep(3000);
System.out.println("3S后数据开始处理中。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Async
public Future<String> asyncInvokeReturnFuture(int i) {
System.out.println("进入asyncInvokeReturnFuture..." + Thread.currentThread().getName());
Future<String> future;
try {
Thread.sleep(3000);
System.out.println("3S后asyncInvokeReturnFuture数据开始处理中。。");
future = new AsyncResult<String>("success:" + i);
} catch (InterruptedException e) {
future = new AsyncResult<String>("error");
}
return future;
}
}
运行一下看看
package com.executor;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@EnableAutoConfiguration
br/>@EnableAutoConfiguration
br/>@SpringBootApplication
br/>@EnableAsync
private final static Logger logger = LoggerFactory.getLogger(ExampleSync.class);
@Autowired
SyncService asyncService;
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleSync.class, args);
}
@GetMapping("/hello")
public String hello(){
System.out.println("进入Controller。。。");
asyncService.hello();
Future<String> future=asyncService.asyncInvokeReturnFuture(300);
String s = null;
try {
s = future.get();
} catch (Exception e){
e.printStackTrace();
}
System.out.println("异步方法返回值 : "+s);
return s;
}
}
进入Controller。。。
进入service。。。
进入asyncInvokeReturnFuture...Anno-Executor2
3S后数据开始处理中。。
3S后asyncInvokeReturnFuture数据开始处理中。。
异步方法返回值 : success:300
进入Controller。。。
进入service。。。
进入asyncInvokeReturnFuture...Anno-Executor3
3S后数据开始处理中。。
3S后asyncInvokeReturnFuture数据开始处理中。。
异步方法返回值 : success:300
文章标题:springboot中使用自定义线程池ThreadPoolTaskExecutor
浏览地址:https://www.cdcxhl.com/article8/ppppip.html
成都网站建设公司_创新互联,为您提供电子商务、企业建站、定制开发、搜索引擎优化、虚拟主机、网站维护
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联