异步和线程池使用

异步和线程池使用

使用场景

在开发过程中会遇到业务复杂,或存在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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值