我应该调用 `executorService.shutdown` 和 `executorService.awaitTermination` 吗?
should I call `executorService.shutdown` and `executorService.awaitTermination`?
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
这个completed execution after a shutdown
是什么意思?
这是否意味着我必须执行 shutdown
API?
我的代码应该是这样的:
@Test
public void setUserNamePropertyFromMultiThreadsUpdatesCorrectly() throws Exception {
..
List<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < 10; i++) {
callables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
..
assertTrue(isSuccess);
return null;
}
});
}
ExecutorService executorService = Executors.newFixedThreadPool(10);
try {
executorService.invokeAll(callables);
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
..
}
或
try {
executorService.invokeAll(callables);
executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
This method does not wait for previously submitted tasks to
complete execution
是什么意思?即使任务未完成也发送停止中断?
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*/
void shutdown();
shutdown
: 没有更多任务。
awaitTermination
: 在关闭请求后调用。
What does this completed execution after a shutdown mean?
你应该先打电话给shutdown
。否则,您可能会等待很长时间,因为 awaitTermination
实际上并没有关闭您的执行程序。
Does it mean I have to execute a shutdown API?
是的,如上所述
What does This method does not wait for previously submitted tasks to
complete execution mean?
这意味着,shutdown
不是阻塞调用...方法 returns 在调用后立即。在一切都完成之前被阻止.. 你打电话给 awaitTermination
.
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
这个completed execution after a shutdown
是什么意思?
这是否意味着我必须执行 shutdown
API?
我的代码应该是这样的:
@Test
public void setUserNamePropertyFromMultiThreadsUpdatesCorrectly() throws Exception {
..
List<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < 10; i++) {
callables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
..
assertTrue(isSuccess);
return null;
}
});
}
ExecutorService executorService = Executors.newFixedThreadPool(10);
try {
executorService.invokeAll(callables);
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
..
}
或
try {
executorService.invokeAll(callables);
executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
This method does not wait for previously submitted tasks to
complete execution
是什么意思?即使任务未完成也发送停止中断?
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*/
void shutdown();
shutdown
: 没有更多任务。
awaitTermination
: 在关闭请求后调用。
What does this completed execution after a shutdown mean?
你应该先打电话给shutdown
。否则,您可能会等待很长时间,因为 awaitTermination
实际上并没有关闭您的执行程序。
Does it mean I have to execute a shutdown API?
是的,如上所述
What does This method does not wait for previously submitted tasks to complete execution mean?
这意味着,shutdown
不是阻塞调用...方法 returns 在调用后立即。在一切都完成之前被阻止.. 你打电话给 awaitTermination
.