java.util.concurrent.ExecutorService:
ExecutorService扩展了Executor并添加了一些生命周期管理的方法,同时提供一个Future用来监控异步任务的执行过程。一个Executor的生命周期有三种状态,运行 ,关闭 ,终止 。Executor创建时处于运行状态。当调用ExecutorService.shutdown()后,处于关闭状态,isShutdown()方法返回true。这时,不应该再向Executor中添加任务,所有已添加的任务执行完毕后,Executor处于终止状态时isTerminated()返回true。如果Executor处于关闭状态,往Executor提交任务会抛出 RejectedExecutionException。
ExecutorService中重要的方法:
1. void shutdown()
按照任务提交的顺序关闭已经提交的任务,不再接受新提交的任务。
2. List<Runnable> shutdownNow()
尝试停止所有正在执行的任务,停止处理所有处在等待状态的任务,返回正在等待执行的任务的列表。
3. boolean isShutdown()
如果执行器(Executor)已经被关闭,则返回true。
4. boolean isTerminated()
如果所有任务都已经被关闭,则返回true。如果之前没有调用shutdown或shutdownNow,即使没有任务在执行也不会返回true。
5. boolean awaitTermination(long timeout, TimeUnit unit)
提交shutdown请求后,等待所有任务执行完毕、发生超时或当前线程被interrupted。当Executor被终止时返回true,当超时发生返回false。
ExecutorService扩展了Executor并添加了一些生命周期管理的方法,同时提供一个Future用来监控异步任务的执行过程。一个Executor的生命周期有三种状态,运行 ,关闭 ,终止 。Executor创建时处于运行状态。当调用ExecutorService.shutdown()后,处于关闭状态,isShutdown()方法返回true。这时,不应该再向Executor中添加任务,所有已添加的任务执行完毕后,Executor处于终止状态时isTerminated()返回true。如果Executor处于关闭状态,往Executor提交任务会抛出 RejectedExecutionException。
ExecutorService中重要的方法:
1. void shutdown()
按照任务提交的顺序关闭已经提交的任务,不再接受新提交的任务。
2. List<Runnable> shutdownNow()
尝试停止所有正在执行的任务,停止处理所有处在等待状态的任务,返回正在等待执行的任务的列表。
3. boolean isShutdown()
如果执行器(Executor)已经被关闭,则返回true。
4. boolean isTerminated()
如果所有任务都已经被关闭,则返回true。如果之前没有调用shutdown或shutdownNow,即使没有任务在执行也不会返回true。
5. boolean awaitTermination(long timeout, TimeUnit unit)
提交shutdown请求后,等待所有任务执行完毕、发生超时或当前线程被interrupted。当Executor被终止时返回true,当超时发生返回false。