你不提交线程执行者,您要提交的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);
}
}
是的,它只是一个方法调用。回来很好。 –