CountDownLatch导致的线程阻塞问题及线程池的使用

2022-06-15 13:09:15

CountDownLatch导致的线程阻塞问题及线程池的使用

CountDownLatch是什么

countdownlatch 是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完毕再执行。从命名可以解读到countdown 是倒数的意思,类似于我们倒计时的概念。countdownlatch 提供了两个方法,一个是 countDown,一个是 await,countdownlatch 初始化的时候需要传入一个整数,在这个整数倒数到 0 之前,调用了 await 方法的程序都必须要等待,然后通过countDown 来倒数。

项目中的应用

我们项目中有查询业绩功能,sql查询次数较多,于是使用多线程查询,但是查询结果需要统一封装返回,于是使用多线程查询的同时辅以CountDownLatch进行线程的约束

问题记录

查询响应较慢,每次响应时间大约都是10秒左右,经查看countDownLatch.await(10,TimeUnit.SECONDS)设定的阻塞时间为10秒,深入代码走查,发现新开的线程中有异常抛出而未捕获,导致线程计数器未按照理想状态下进行countDowm,最终过10秒线程主动向下执行导致的。

ThreadFactory threadFactory=Executors.defaultThreadFactory();CountDownLatch countDownLatch=newCountDownLatch(4);
threadFactory.newThread(()->{Resps[0]=getDistributionPerformanceOfcurDay(req);
    countDownLatch.countDown();}).start();
threadFactory.newThread(()->{Resps[1]=getDistributionPerformanceOfWeek(req);
    countDownLatch.countDown();}).start();
threadFactory.newThread(()->{Resps[2]=getDistributionPerformanceOfMonth(req);
    countDownLatch.countDown();}).start();
threadFactory.newThread(()->{Resps[3]=getDistributionPerformanceOfPreMonth(req);
    countDownLatch.countDown();}).start();

上述每个线程中都是查询语句,分别查询当天、当周、当月以及上月的业绩。

在每个线程中都加上try-catch,finally中执行countDownLatch.countDown();解决!

学习下CountDownLatch以及线程池

jdk封装的线程池问题
  • FixedThreadPool 和 SingleThreadPool:
    允许的请求队列长度为 Integer.MAX_VALUE ,会堆积大量请求OOM
  • CachedThreadPool 和 ScheduledThreadPool:
    允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量线程OOM
创建线程池
publicThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler)

虽然可以这么写,但是我可能需要在项目中使用很多个线程池,如果用的时候就新建,那岂不是跟new Thread的做法一样了?

所以自定义一个线程池,每次用这个线程池的时候保证只有一个,也就是搞成单例模式的,就是代码中可以随时取用,但是返回的永远是同一个。

publicclassMyThreadPool{privatestaticvolatileThreadPoolExecutor threadPoolExecutor=null;publicstaticThreadPoolExecutorgetThreadPool(){if(threadPoolExecutor!=null){return threadPoolExecutor;}synchronized(MyThreadPool.class){if(threadPoolExecutor==null){

                threadPoolExecutor=newThreadPoolExecutor(10,10,300L,TimeUnit.SECONDS,newLinkedBlockingQueue<Runnable>(100),newThreadPoolRejectHandler());

                threadPoolExecutor.allowCoreThreadTimeOut(true);//任务执行完之后退出线程池}}return threadPoolExecutor;}publicstaticclassThreadPoolRejectHandlerimplementsRejectedExecutionHandler{@OverridepublicvoidrejectedExecution(Runnable r,ThreadPoolExecutor executor){// 可以直接抛异常,记录日志// 可以另外起一个临时线程处理这个被决绝的任务}}}
  • 线程池的线程数量长期维持在 corePoolSize 个(核心线程数量)
  • 线程池的线程数量最大可以扩展到 maximumPoolSize 个
  • 在 corePoolSize ~ maximumPoolSize 这个区间的线程,一旦空闲超过keepAliveTime时间,就会被杀掉(时间单位)
  • 送来工作的线程数量超过最大数以后,送到 workQueue 里面等号
  • 等号的队列也满了,就按照事先约定的策略 RejectedExecutionHandler 给拒绝掉
  • 业务逻辑中调用时直接MyThreadPool.getThreadPool()即可
线程池的拒绝策略-默认4种

源码如下(加了注释),列出四种默认的拒绝策略

自己也可以定义个拒绝策略,实现RejectedExecutionHandler即可,然后在新建的线程池中加进去就行。我上面贴出来的自定义的线程池就是

publicstaticclassCallerRunsPolicyimplementsRejectedExecutionHandler{/**
     *  在调用者线程中(也就是说是谁把任务放进线程池)运行当前被丢弃的任务。

	 *	只会用调用者所在线程来运行任务,也就是说任务不会进入线程池。

	 *	如果线程池已经被关闭,则直接丢弃该任务。
     */publicCallerRunsPolicy(){}/**
     * Executes task r in the caller's thread, unless the executor
     * has been shut down, in which case the task is discarded.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     */publicvoidrejectedExecution(Runnable r,ThreadPoolExecutor e){if(!e.isShutdown()){
            r.run();}}}/**
 * A handler for rejected tasks that throws a
 * {@link RejectedExecutionException}.
 * 创建线程池时如若不指定拒绝策略,默认的就是这个
 * 直接抛出拒绝异常,会中断调用者的处理过程
 * This is the default handler for {@link ThreadPoolExecutor} and
 * {@link ScheduledThreadPoolExecutor}.
 */publicstaticclassAbortPolicyimplementsRejectedExecutionHandler{/**
     * Creates an {@code AbortPolicy}.
     */publicAbortPolicy(){}/**
     * Always throws RejectedExecutionException.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     * @throws RejectedExecutionException always
     */publicvoidrejectedExecution(Runnable r,ThreadPoolExecutor e){thrownewRejectedExecutionException("Task "+ r.toString()+" rejected from "+
                                             e.toString());}}/**
 * A handler for rejected tasks that silently discards the
 * rejected task.
 * 默默的丢弃这个被拒绝的任务。英文底子好真棒!
 */publicstaticclassDiscardPolicyimplementsRejectedExecutionHandler{/**
     * Creates a {@code DiscardPolicy}.
     */publicDiscardPolicy(){}/**
     * Does nothing, which has the effect of discarding task r.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     */publicvoidrejectedExecution(Runnable r,ThreadPoolExecutor e){}}/**
 * A handler for rejected tasks that discards the oldest unhandled
 * request and then retries {@code execute}, unless the executor
 * is shut down, in which case the task is discarded.
 * 丢弃队列中最老的,然后再次尝试提交新任务
 */publicstaticclassDiscardOldestPolicyimplementsRejectedExecutionHandler{/**
     * Creates a {@code DiscardOldestPolicy} for the given executor.
     */publicDiscardOldestPolicy(){}/**
     * Obtains and ignores the next task that the executor
     * would otherwise execute, if one is immediately available,
     * and then retries execution of task r, unless the executor
     * is shut down, in which case task r is instead discarded.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     */publicvoidrejectedExecution(Runnable r,ThreadPoolExecutor e){if(!e.isShutdown()){
            e.getQueue().poll();
            e.execute(r);}}}
  • 作者:会中流击水
  • 原文链接:https://blog.csdn.net/weixin_39515118/article/details/123875749
    更新时间:2022-06-15 13:09:15