自己看的,亂寫(xiě)的,勿噴
線程配置類(lèi)
package mau5.top.myproject.common.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ThreadAsyncConfigurer implements AsyncConfigurer {
private final static Logger log = LoggerFactory.getLogger(ThreadAsyncConfigurer.class);
@Bean
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
// 設(shè)置核心線程數(shù)
threadPool.setCorePoolSize(3);
// 設(shè)置最大線程數(shù)
threadPool.setMaxPoolSize(8);
// 線程池所使用的緩沖隊(duì)列
threadPool.setQueueCapacity(10);
// 等待任務(wù)在關(guān)機(jī)時(shí)完成--表明等待所有線程執(zhí)行完
threadPool.setWaitForTasksToCompleteOnShutdown(true);
// 等待時(shí)間 (默認(rèn)為0,此時(shí)立即停止)鸥昏,并沒(méi)等待xx秒后強(qiáng)制停止
threadPool.setAwaitTerminationSeconds(60);
threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化線程
threadPool.initialize();
return threadPool;
}
/**
* 異常處理
*
* @return
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new CustomAsyncExceptionHandler();
}
/**
* 自定義異常處理類(lèi)
*/
class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
log.error("==========================" + throwable.getMessage() + "=======================", throwable);
log.error("exception method:" + method.getName());
for (Object param : obj) {
log.error("Parameter value - " + param);
}
}
}
}
寫(xiě)一個(gè)Callable
實(shí)現(xiàn)類(lèi)實(shí)現(xiàn)業(yè)務(wù)
package mau5.top.myproject.business.callable;
import java.util.concurrent.*;
public class TestCallable implements Callable {
@Override
public Object call() throws Exception {
int random = (int) (Math.random() * 10000);
System.out.println("隨機(jī)生成數(shù):" + random);
Thread.sleep(random);
return random;
}
}
調(diào)用Callable
實(shí)現(xiàn)類(lèi)
package mau5.top.myproject.business.controller;
import mau5.top.myproject.business.callable.TestCallable;
import mau5.top.myproject.business.entity.po.CommodityCategory;
import mau5.top.myproject.business.service.ICommodityCategoryService;
import mau5.top.myproject.common.config.ThreadAsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@RestController
@RequestMapping("/cate")
public class CommodityCategoryController {
@Resource
private ThreadAsyncConfigurer threadAsyncConfigurer;
@GetMapping("/testCallable")
public Object callable(){
try {
long currentTimeMillis = System.currentTimeMillis();
TestCallable callable = new TestCallable();
// 手動(dòng)創(chuàng)建線程
// ExecutorService executor = Executors.newFixedThreadPool(3);
// 使用線程池
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor)threadAsyncConfigurer.getAsyncExecutor();
Future<Integer> submit1 = executor.submit(callable);
Future<Integer> submit2 = executor.submit(callable);
Future<Integer> submit3 = executor.submit(callable);
int s = submit1.get() + submit2.get() +submit3.get();
long currentTimeMillis2 = System.currentTimeMillis();
System.out.println("總共時(shí)間:" + s);
System.out.println("耗時(shí):" + (currentTimeMillis2 - currentTimeMillis));
// 非線程池時(shí)才用shutdown
// executor.shutdown();
return "總共時(shí)間:" + s + ",耗時(shí):" + (currentTimeMillis2 - currentTimeMillis);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}