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

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)

  1. private ThreadPoolExecutor m_jobExecutor;  
  2.   
  3. void tryToExecute(Job job)  
  4. {  
  5.       try  
  6.         {  
  7.             m_jobExecutor.execute(job);  
  8.         } catch (RejectedExecutionException e)  
  9.         {  
  10.             // Iterate through all processors starting from random point   
  11.             List allProcessors = getAll();  
  12.               
  13.             int startIndex = m_random.nextInt(allProcessors.size());  
  14.             int i = startIndex;  
  15.             do  
  16.             {  
  17.                 Processor processor = allProcessors.get(i);  
  18.   
  19.                 i = (i + 1) % allProcessors.size();  
  20.                   
  21.                 if (processor == this)  
  22.                 {  
  23.                     continue;  
  24.                 }  
  25.                 if (processor.lendAWorkerThread(job))  
  26.                 {  
  27.                     // We found a Good Samaritan  
  28.                     log.info(getName() + " has lent a worker thread from the "  
  29.                             + processor.getName());  
  30.                     return;  
  31.                 }  
  32.             } while (i != startIndex);   
  33.             // Nobody helped us, re-throw the exception  
  34.             throw e;  
  35.         }  
  36. }  
线程租借条件

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

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

  1. boolean lendAWorkerThread(LocateJob job)  
  2. {  
  3.     if (!isStartingOrStarted())  
  4.     {  
  5.         return false;  
  6.     }  
  7.   
  8.     int maxPoolSize = m_jobExecutor.getMaximumPoolSize();  
  9.   
  10.     if (maxPoolSize - getSuggestedNumberOfWorkerThreads() < (maxPoolSize / 10))  
  11.     {  
  12.         // We do not have capacity  
  13.         return false;  
  14.     }  
  15.     if ((maxPoolSize - m_jobExecutor.getActiveCount()) < (maxPoolSize / 10))  
  16.     {  
  17.         // We have capacity but we lent (or consumed) almost all of it  
  18.         return false;  
  19.     }  
  20.     try  
  21.     {  
  22.         m_jobExecutor.execute(job);  
  23.     } catch (Throwable e)  
  24.     {  
  25.         // We couldn't lent a worker thread  
  26.         return false;  
  27.     }  
  28.       
  29.     return true;  
  30. }  
线程池什么时候会抛出RejectedExecutionException?

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

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

  1. m_jobExecutor = new ThreadPoolExecutor(m_config.getThreadPoolMaxSize(),  
  2.               m_config.getThreadPoolMaxSize(), getPollingIntervalInMillis(),  
  3.               TimeUnit.MILLISECONDS, new SynchronousQueue(),  
  4.               new DaemonThreadFactory(getName()))  
  5.       {  
  6.           protected void beforeExecute(Thread t, Runnable r)  
  7.           {  
  8.               super.beforeExecute(t, r);  
  9.   
  10.               try  
  11.               {  
  12.                   (Job) r).setExecutionThread(t);  
  13.               } catch (Throwable e)  
  14.               {  
  15.                   getLog().error(getName() + ": internal error: " + e);  
  16.               }  
  17.           }  
  18.   
  19.           protected void afterExecute(Runnable r, Throwable t)  
  20.           {  
  21.               super.afterExecute(r, t);  
  22.           }  
  23.       };  
  24.       m_jobExecutor.prestartAllCoreThreads();  
  25.       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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值