Java ThreadPoolExecutor 线程池 tips 2:偷借线程

14 篇文章 0 订阅

SEDA (Staged event-driven architecture)

论文在此: The Staged Event-Driven Architecture for Highly-Concurrent Server Applications

尚未阅读,从字面理解

  • 有event, 则有event queue
  • 有event, 则有event handler
  • 有Staged,划分成不同阶段,将任务进行细分,每个细分的阶段使用不一样的线程池配置

租借线程

简而言之,概而括之,推而广之,系统中不是单个线程池来运行系统中的任务线程,而是多个线程池来handle不同类型的任务,比如数据库的连接,文件IO的读写,网络IO的读写,对象的序列化等等。

不管是单个线程池还是多个线程池,池的配置毫无疑问影响池的性能和效用。在前一篇文章中,我提到单线程吞吐量来估计系统的线程数目(suggestedNumberOfWorkerThread)。本篇文章会介绍另外一个小技巧,线程的租借。

运行任务

如果当前的线程池(m_jobExecutor)抛出RejectedExecutionExecution,则尝试从其他线程池(Processor)租借(lendAWorkerThread)

private ThreadPoolExecutor m_jobExecutor;

void tryToExecute(Job job)
{
      try
        {
            m_jobExecutor.execute(job);
        } catch (RejectedExecutionException e)
        {
            // Iterate through all processors starting from random point 
            List allProcessors = getAll();
            
            int startIndex = m_random.nextInt(allProcessors.size());
            int i = startIndex;
            do
            {
                Processor processor = allProcessors.get(i);

                i = (i + 1) % allProcessors.size();
                
                if (processor == this)
                {
                    continue;
                }
                if (processor.lendAWorkerThread(job))
                {
                    // We found a Good Samaritan
                    log.info(getName() + " has lent a worker thread from the "
                            + processor.getName());
                    return;
                }
            } while (i != startIndex); 
            // Nobody helped us, re-throw the exception
            throw e;
        }
}

线程租借条件

如果最大线程池大小和预估使用线程数目(suggestedNumberOfWorkerThread)相差无几(10%),说明当下线程池也将会很繁忙,不能租借。

如果当前线程池中的已经90%的active (m_jobExecutor.getActiveCount()),说明当下线程池已经很繁忙,不能租借。

    boolean lendAWorkerThread(LocateJob job)
    {
        if (!isStartingOrStarted())
        {
            return false;
        }

        int maxPoolSize = m_jobExecutor.getMaximumPoolSize();

        if (maxPoolSize - getSuggestedNumberOfWorkerThreads() < (maxPoolSize / 10))
        {
            // We do not have capacity
            return false;
        }
        if ((maxPoolSize - m_jobExecutor.getActiveCount()) < (maxPoolSize / 10))
        {
            // We have capacity but we lent (or consumed) almost all of it
            return false;
        }
        try
        {
            m_jobExecutor.execute(job);
        } catch (Throwable e)
        {
            // We couldn't lent a worker thread
            return false;
        }
        
        return true;
    }

线程池什么时候会抛出RejectedExecutionException?

当线程池使用了有界队列和有界池,队列已经被填满,池中的线程也已经全部在运行。(参见javadoc  reject task)

本文的ThreadPoolExecutor使用SynchronousQueue,只要线程池被耗尽,新增任务就会抛出RejectedExecutionException

  m_jobExecutor = new ThreadPoolExecutor(m_config.getThreadPoolMaxSize(),
                m_config.getThreadPoolMaxSize(), getPollingIntervalInMillis(),
                TimeUnit.MILLISECONDS, new SynchronousQueue(),
                new DaemonThreadFactory(getName()))
        {
            protected void beforeExecute(Thread t, Runnable r)
            {
                super.beforeExecute(t, r);

                try
                {
                    (Job) r).setExecutionThread(t);
                } catch (Throwable e)
                {
                    getLog().error(getName() + ": internal error: " + e);
                }
            }

            protected void afterExecute(Runnable r, Throwable t)
            {
                super.afterExecute(r, t);
            }
        };
        m_jobExecutor.prestartAllCoreThreads();
        m_jobExecutor.allowCoreThreadTimeOut(true);

线程池的动态调整

在下一篇文章,我将介绍一种超级大杀器: sliding windows statistical,根据过去线程的使用状况,来配置下一个windows窗口的线程池(run time suggestedNumberOfWorkerThread),从而形成弹性线程池(elastic thread pool),在系统访问高峰时期,自动增加线程池的数目;在peak回落时,自动减少线程池的数目。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FireCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值