1.介绍
公司项目中用到了springboot自带的org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor进行线程池异步调用,封装好的工具类,记录一下备用
2.代码(AsyncUtils.java)
package cn.gzsendi.utils;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
public class AsyncUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncUtils.class);
private static final ThreadPoolTaskExecutor EXECUTOR;
static {
EXECUTOR = new ThreadPoolTaskExecutor() {
private static final long serialVersionUID = -1644198965440814899L;
@Override
protected ExecutorService initializeExecutor(ThreadFactory threadFactory,
RejectedExecutionHandler rejectedExecutionHandler) {
try {
return this.getThreadPoolExecutor();
} catch (IllegalStateException ise) {
}
return super.initializeExecutor(threadFactory, rejectedExecutionHandler);
}
};
EXECUTOR.setCorePoolSize(2);
EXECUTOR.setMaxPoolSize(10);
EXECUTOR.setAllowCoreThreadTimeOut(true);
EXECUTOR.setWaitForTasksToCompleteOnShutdown(true);
EXECUTOR.setThreadNamePrefix("AsyncUtils_");
EXECUTOR.setTaskDecorator(AsyncUtils::wrapRunnable);
EXECUTOR.setBeanName("AsyncUtils_TaskExecutor");
EXECUTOR.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
EXECUTOR.afterPropertiesSet();
}
@Bean("AsyncUtils_TaskExecutor")
@Primary
public static ThreadPoolTaskExecutor getExecutor() {
return EXECUTOR;
}
public static void shutdownExecutor() {
EXECUTOR.setWaitForTasksToCompleteOnShutdown(true);
EXECUTOR.shutdown();
}
public static void shutdownExecutorNow() {
EXECUTOR.setWaitForTasksToCompleteOnShutdown(false);
EXECUTOR.shutdown();
}
public static void asyncExecute(Runnable runnable) {
if (runnable == null) {
return;
}
EXECUTOR.execute(runnable);
}
public static <T> Future<T> asyncSubmit(Callable<T> callable) {
if (callable == null) {
return null;
}
return EXECUTOR.submit(wrapCallable(callable));
}
public static void asyncExecute(String threadName, Runnable runnable) {
if (runnable == null) {
return;
}
new Thread(wrapRunnable(runnable), threadName).start();
}
private static Runnable wrapRunnable(Runnable runnable) {
return () -> {
try {
runnable.run();
} catch (Throwable t) {
LOGGER.error("异步线程执行异常 : ", t);
}
};
}
private static <T> Callable<T> wrapCallable(Callable<T> callable) {
return () -> {
try {
return callable.call();
} catch (Throwable t) {
LOGGER.error("异步线程执行异常 : ", t);
throw t;
}
};
}
}
3.测试
public static void main(String[] args) throws IOException, InterruptedException {
AsyncUtils.asyncExecute(()->{
System.out.println("表达式写法执行------");
});
AsyncUtils.asyncExecute(new Runnable() {
public void run() {
System.out.println("普通的写法执行------");
}
});
System.out.println("1111");
}
测试结果:
表达式写法执行------
1111
普通的写法执行------
|