这里写自定义目录标题
异步和线程池使用
使用场景
在开发过程中会遇到业务复杂,或存在for循环处理逻辑的代码,这样会导致方法执行时间很长,想要减少执行时间,就需要用到mq,异步,线程池等技术
线程池使用场景主要是对于for循环处理大量数据或者一次性拉取/处理大量数据
mq/异步使用场景主要是落库或者不影响主流程的业务代码
线程池
创建线程池有两种方式,1、ThreadPoolExecutor jdk线程池;2、ThreadPoolTaskExecutor spring线程池
1、ThreadPoolExecutor线程池创建
@Bean(name = "initPrintThreadPool")
public ThreadPoolExecutor initPrintThreadPool() {
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(threadPoolQueueCapacity);
ThreadFactory threadFactory = new NameTreadFactory();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
threadPoolCorePoolSize, // 核心线程 10
threadPoolMaxPoolSize, // 最大线程 20
threadPoolKeepAliveSeconds, // 空闲时间 60
TimeUnit.SECONDS, // 空闲时间单位
workQueue, // 队列长度 10000
threadFactory ,
new ThreadPoolExecutor.DiscardPolicy()); // 拒绝策略,丢弃任务,但是不抛出异常
threadPoolExecutor.prestartAllCoreThreads(); // 预启动所有核心线程
return threadPoolExecutor;
/**
* 线程信息
*/
static class NameTreadFactory implements ThreadFactory {
private final AtomicInteger mThreadNum = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "number-thread-" + mThreadNum.getAndIncrement());
}
}
}
执行runnable不需要返回值线程
threadPool.execute(new Runnable(param));
执行callable需要返回值线程
1、执行一个线程
Future futures = threadPool.submit(new ExpressSheetSubscriber(param));
1、阻塞线程,等待所有线程返回结果
List expressSheetSubscribers= new ArrayList<>();
expressSheetSubscribers.add(new ExpressSheetSubscriber(param))
List<Future> futures = threadPool.invokeAll(expressSheetSubscribers);
2、ThreadPoolTaskExecutor线程池
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setThreadNamePrefix(THREAD_NAME_PREFIX);
threadPoolTaskExecutor.setCorePoolSize(threadPoolCorePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(threadPoolMaxPoolSize);
threadPoolTaskExecutor.setQueueCapacity(threadPoolQueueCapacity);
threadPoolTaskExecutor.setKeepAliveSeconds(threadPoolKeepAliveSeconds);
// 线程池对拒绝任务(无线程可用)的处理策略,丢弃任务,但是不抛出异常
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
return threadPoolTaskExecutor;
}
执行runnable不需要返回值线程
threadPool.execute(new Runnable(param));
执行callable需要返回值线程
1、执行一个线程
Future futures = threadPool.submit(new ExpressSheetSubscriber(param));
异步EventBus
eventBus必要配置
1、类注解@AsyncEventSubscriber
2、观察者监听注解@Subscribe @AllowConcurrentEvents
在设置观察者时,需要使用注解类@Subscribe来标识一个订阅者,但在注解中还要一个注解@AllowConcurrentEvents,这个注解是用来标识当前订阅者是线程安全的
3、监听类 Event
4、发布事件 SpringContextUtils.getBean(AsyncEventBus.class).post(event);
发布事件工具类
public final class EventUtils {
public static void postEvent(Object event) {
SpringContextUtils.getBean(AsyncEventBus.class).post(event);
}
}
监听类
@AsyncEventSubscriber
@Component
public class Subscriber {
@Subscribe
@AllowConcurrentEvents
public void save(Event event) {
System.out.println(event);
}
}
调用
Event event = new Event();
event.setName("张三");
EventUtil.postEvent(event);
监听类
public class Event {
private String name;
}