你不提交線程執行者,您要提交的Runnable吧。在Runnable中調用返回不會導致執行它的線程終止。遺囑執行人是這麼寫的,它可以在的Runnable的形式運行多個任務,當一個Runnable完成執行(不管它是否早返回或其他)的線程將繼續,並且會從它的排隊等候的更多的工作提交的任務。
下面是在的ThreadPoolExecutor#runWorker方法的代碼。顯示task.run()
的行是工作線程執行任務的地方,當您的任務返回時,工作人員的執行從此處開始。
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) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
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) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
是的,它只是一個方法調用。回來很好。 –