사용자 정의 스 레 드 탱크 ThreadPoolExecutor

8739 단어 ANDROIDJAVA
public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
Added in API level 1
Creates a new ThreadPoolExecutor with the given initial parameters.
Parameters
corePoolSize
the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
maximumPoolSize
the maximum number of threads to allow in the pool
keepAliveTime
when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
unit
the time unit for the keepAliveTime argument
workQueue
the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
threadFactory
the factory to use when the executor creates a new thread
handler
the handler to use when execution is blocked because the thread bounds and queue capacities are reached
Throws
IllegalArgumentException
if one of the following holds: corePoolSize < 0 keepAliveTime < 0 maximumPoolSize <= 0 maximumPoolSize < corePoolSize
NullPointerException
if workQueue or threadFactory or handler is null
실례:
BlockingQueue sPoolWorkQueue = new LinkedBlockingQueue(3);//fixedpool or singlepool
Executor executor = new ThreadPoolExecutor(2, 4, 0, TimeUnit.SECONDS, sPoolWorkQueue);
 
  
corePoolSize=1:
     executor              (LinkedBlockingQueue     :2) corePoolSize   ,    corePoolSize   。
  ,   ,
executor.execute(r1);//r1  Runnable  。
 
  
executor.execute(r2);
 
  
executor.execute(r3);
 
  
executor.execute(r4);
 
  
executor.execute(r5);
  ,      ,   r1,r2,  r3,r4,r5      ,r1,r2    ,  r1,r2      r3,r4,  r3,r4             r5.
1、        2   .(  corePoolSize 2,      <=corePoolSize ,  new Thread())
2、executor       5,    r1,r2,    r3,r4,r5,        3,      r3,r4,r5.     corePoolSize   ,        。
                  ?         :
 
  
maximumPoolSize=4:
   executor         corePoolSize+       ,   corePoolSize  ,  maximunPoolSize          。
  ,   ,
 
  
executor.execute(r1);
 
  
executor.execute(r2);
 
  
executor.execute(r3);
executor.execute(r4);
 
  
executor.execute(r5);
executor.execute(r6);
 
  
executor.execute(r7);
    corePoolSize   ,        ,     5   ,      。  (   RejectedExecutionHandler )    。
    : corePoolSize     maximumPoolSize, ,   <=maximumPoolSize,  new Thread().
  ,    :   r1,r2,r3,r4,   r5,r6,r7      , r1,r2,r3,r4    ,           r5,r6,r7.
  ,  executor  8   ,               。    ?       :
 
  
handler=new AbortPolicy():
AbortPolicy,  RejectedExecutionHandler  ,        。     handler  。
CallerRunsPolicy,       “try again”.(   ...)。  :          ,   NetworkOnMainThreadException
DiscardPolicy,       “              ”.(   ...)
DiscardOldestPolicy,       “                ,        ”.(   ...)
  CallerRunsPolicy,          ,                     。      maximumPoolSize   ,     ,      maximumPoolSize+1     ,       ,   maximunPoolSize+1     ,     maximumPoolSize       。
                    ,      。
 ,DiscardPolicy DiscardOldestPolicy             ,     。

1、        LinkedBlockingQueue      Integer.MAX_VALUE,    maximumPoolSize    ,      corePoolSize。
LinkedBlockingQueue  ,        ,  maximumPoolSize       maximumPoolSize=corePoolSize.
  Executors  newFixedThreadPool newSingleThreadExecutor corePoolSize maximumPoolSize     。   newFixedThreadPool newSingleThreadExecutor corePoolSize maximumPoolSize          ,                。

2、Executors    newCachedThreadPool()            ,newFixedThreadPool(int),newSingleThreadPool newScheduledThreadPool()          。
    :
newFixedThreadPool(int),newSingleThreadPool,       。     。
newCachedThreadPool(),        。        。
    :
newFixedThreadPool() newSingleThreadPool,      LinkedBlockingQueue,               ,   ,    LinkedBlockingQueue  ,  keepAliveTime    。
newCachedThreadPool,  SynchronousQueue  ,     keepAliveTime,   corePoolSize  。              。
SynchronousQueue          。  maximumPoolSize    ,corePoolSize  ,    0。(   newCachedThreadPool corePoolSize  0   )         maximumPoolSize,     java.lang.StackOverflowError.      ,   SynchronousQueue    corePoolSize  。
LinkedBlockingQueue        keepAliveTime ,     allowCoreThreadTimeOut(true) ,   keepAliveTime.       ,  keepAliveTime    。

    ,newCachedThreadPool    ,              ,     ,        。
      APP,   “       ”   ,    newFixedThreadPool()     。          ,     。
          ,    5      。
  ,  APP        ,            5     。       newFixedThreadPool()       。

좋은 웹페이지 즐겨찾기