スレッドプールを作成する1.ThreadPoolExecutorコンストラクタ
公共ThreadPoolExecutor(INT corePoolSize、INT maximumPoolSize、長いkeepAliveTimeが、TimeUnitでユニット、BlockingQueueの<Runnableを>ワークキュー、ThreadFactory threadFactory、ハンドラのRejectedExecutionHandler){(corePoolSize <0 || maximumPoolSize <= 0 || maximumPoolSize <corePoolSize || keepAliveTimeが<0)であれば新しいIllegalArgumentExceptionをスローし(); もし(ワークキュー== nullの|| threadFactory == nullの||ハンドラ== null)の新しいNullPointerExceptionが(スロー)。this.acc = System.getSecurityManager()== nullの?ヌル:AccessController.getContext(); this.corePoolSize = corePoolSize。this.maximumPoolSize = maximumPoolSize。this.workQueue =ワークキュー; this.keepAliveTime = unit.toNanos(keepAliveTimeが)。this.threadFactory = threadFactory。this.handler =ハンドラ。}
参数简介:
- corePoolSize:核心线程数量
- maximumPoolSize:最大线程数量
- keepAliveTime:没有执行任务的线程,最大空闲时间
- unit:时间单位,TimeUnit.DAYS,TimeUnit.HOURS,TimeUnit.MINUTES,TimeUnit.SECONDS,TimeUnit.MILLISECONDS
TimeUnit.MICROSECONDS,TimeUnit.NANOSECONDS,七种
- BlockingQueue<Runnable> workQueue:一个阻塞队列,用来存储等待执行的任务, ArrayBlockingQueue,LinkedBlockingQueue,SynchronousQueue三种 - ThreadFactory threadFactory:创建线程的工厂类 - RejectedExecutionHandler handler:拒绝策略
ごみ戦略の4種類を達成するために:政策を否定することは2.戦略は否定してのRejectedExecutionHandlerインタフェース1.AbortPolicy(戦略アボート、例外がスローされ、デフォルトのポリシー)
源码:{@code RejectedExecutionException} *をスロー拒否されたタスクのために/ ** * Aハンドラ。 /パブリック静的クラスはAbortPolicy実装のRejectedExecutionHandler {/ * * {@code AbortPolicy}を作成します。* /パブリックAbortPolicy(){}
/**
* Always throws RejectedExecutionException.
*
* [@param](https://my.oschina.net/u/2303379) r the runnable task requested to be executed * [@param](https://my.oschina.net/u/2303379) e the executor attempting to execute this task * [@throws](https://my.oschina.net/throws) RejectedExecutionException always */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); } }
2.CallerRunsPolicy(ハンドルを呼び出すスレッドによって、このタスク)
源码:{@codeを実行}メソッドの呼び出したスレッドに直接拒否タスク*を実行拒否タスクの/ ** * Aハンドラは、*エグゼキュータは、その場合、タスクは、*破棄され、シャットダウンされていない限り。 /パブリック静的クラスはCallerRunsPolicy実装のRejectedExecutionHandler {/ * * {@codeのCallerRunsPolicy}を作成します。* /パブリックCallerRunsPolicy(){}
/**
* 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
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
3.DiscardOldestPolicy(キューの先頭には、タスクを破棄し、ジョブが拒否され、再提出されました)
源码:/ ** *キュータない限り、最も古い未処理*要求し、その後再試行を{@codeが実行}廃棄拒否タスクのハンドラ*は、タスクが破棄された場合にシャットダウン、です。 /パブリック静的クラスはDiscardOldestPolicy実装のRejectedExecutionHandler {/ * *与えられたエグゼキュータ用{@codeのDiscardOldestPolicy}を作成します。* /パブリックDiscardOldestPolicy(){}
/**
* 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
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
4.DiscardPolicy(廃棄タスクが、例外をスローすることはありません。スレッドキューがいっぱいの場合、以降の提出タスクは破棄され、そして静かに破棄されます)
源码:黙って破棄は*タスクを拒否したことを拒否されたタスクのために/ ** *ハンドラ。 /パブリック静的クラスはDiscardPolicy実装のRejectedExecutionHandler {/ * * {@codeのDiscardPolicy}を作成します。* /パブリックDiscardPolicy(){}
/**
* 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
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { } }
実際のニーズによると、拒否した適切な戦略を開発