ここではJavaのスレッドプールの使用量は、ちょうどThreadPoolExecutorの原則のいくつかを言う、と言うことではありません。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
corePoolSize:コア・プールのサイズ
maximumPoolSize:スレッドプール内のスレッドの最大数
keepAliveTimeが次の場合、タスクの実行を終了していないスレッドを維持するまでを占めますどのくらい
ワークキュー:ブロッキングキューが実行されるのを待っているタスクを格納するために使用されます
threadFactory:メインスレッドの作成に使用するスレッドファクトリ
ハンドラ:でタスクに対処することを拒否したときにポリシーを表します
私の関心は、二つの場所であります
1:タスクが管理する方法であります
2:スレッドプールのスレッドは、どのように有効にする再利用と、タイムアウトメカニズムを管理する方法であります
この知識と基本的に2つのメインスレッドプールは知ることができます。
上から見たワークキューは、ストア・タスクが実行されるのを待っているために使用され
1:任务是如何管理的
public void execute(Runnable command) {
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {//当前运行的线程是否小于核心线程数量
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {//判断当前线程池是否处于运行状态,及将任务放入workQueue中
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}:
2:那么线程是如何管理的呢?
addWorker中のスレッドは、ワーク内部に配置され、次いで以下の作品にされています。
private boolean addWorker(Runnable firstTask, boolean core) {
...........................
Worker w = null;
try {
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
................................
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
t.start();
workerStarted = true;
...........................
}
あなたがスレッドプール内の新しいスレッドがアイドル状態ではないしている場合は、新しいスレッドと労働者のリスト内のスレッドにスレッドを作成します。
HashSet<Worker> workers = new HashSet<>();
3:スレッドがどのように再利用?
t.start();后会调用work里面的run
public void run() {
runWorker(this);
}
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {//如果task不为空,那么运行当前的task,如果为空那么在getTask中从workQueue中获取 task
w.lock();
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
.......
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
タスクが空でない場合は、現在のタスクを実行することがでgetTaskでワークキューから空の場合は、タスクを取得します。
あなたが現在アイドル状態のタスクを持っている場合は、新しいタスクが、その後の仕事は新しいタスクを取得することができますワークキューに直接来る、新しいタスクがアイドル状態のスレッドで直接実行されます。この方法は、スレッドプールスレッドの多重化を使用することです。
4:どのようにタイムアウトスレッドが終了?
runWorker里面task = getTask() 从workQueue获取task。我们看看getTask做了是
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
............................
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
タイムアウトがnullを返しますならば、それはプロセスの中核ではない場合、それはworkQueue.poll(keepAliveTimeが、TimeUnit.NANOSECONDS)取得タスクを呼び出します、keepAliveTimeがは、タイムアウトで、runWorkerにされ、タスクがnullの場合、タスク= nullを返します!| |!(タスク= getTask())= nullを、それが偽で、それが終了スレッドを行って内部processWorkerExit、に直接移動します。
final void runWorker(Worker w) {
.......................
try {
while (task != null || (task = getTask()) != null) {
.....................
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
private void processWorkerExit(Worker w, boolean completedAbruptly) {
......................
try {
completedTaskCount += w.completedTasks;
workers.remove(w);
} finally {
mainLock.unlock();
}
.......................
}
}
実行のスレッドが終わったので、スレッドはGCの外に回収されるので、内部から取り出した作品でワットProcessWorkerExit。
ご質問がある場合は、それを指摘してください、ありがとうございました