The previous article talked about the design of the executor interface, which was designed to decouple the task's operation from the submission of the task. Ability to hide a task's run policy. This article says Executorservice interface. It expands the executor interface to manage the life cycle of the executor. and has been further expanded.
Executor is responsible for performing the task.
It has a life cycle of 3: Execute, Close, and terminate.
At any point in time of execution, some tasks may be completed, some may be executing, and some may be waiting to be executed in the queue. So suppose you want to close executor. There are a number of ways, such as graceful smooth closing, not accepting a new task request when execution is closed, and then shutting down executor when the executed task is finished and closing the executor, for example, the most brutal shutdown, regardless of whether the executed task is completed.
1. Shutdown () method is graceful to close the way
2. Shutdownnow () is the way of rough closing. It returns a list of tasks that have not yet started to run.
For a running task, it uses the Thread.Interrupt () method to cancel the task.
Assuming that the task does not respond to a thread's interruption, the task may run continuously.
3. The Awaittermination () method waits for some time to terminate the running task
Public interface Executorservice extends Executor {void shutdown (); List<runnable> Shutdownnow (); Boolean IsShutDown (); Boolean isterminated (); Boolean awaittermination (long timeout, timeunit unit) throws Interruptedexception; <T> future<t> Submit (callable<t> Task); <T> future<t> Submit (Runnable task, T result); Future<?> Submit (Runnable Task); <T> list<future<t>> InvokeAll (collection<?Extends callable<t>> tasks) throws interruptedexception; <T> list<future<t>> InvokeAll (collection<? extends callable<t>> tasks, Long timeout, timeunit unit) throws Interruptedexception; <T> T invokeany (collection<? extends callable<t>> tasks) throws Interruptedexception, Executionex ception; <T> T invokeany (collection<?
Extends callable<t>> tasks, long timeout, timeunit unit) throws Interruptedexception, E Xecutionexception, TimeoutException;}
This code in Executorservice's Javadoc shows a two-stage way to turn off executor.
1. First use shutdown () to stop accepting new tasks
2. Use the Awaittermination () method to wait for a period of time for the running task to finish running. Then Shutdownnow () forcibly closed (using Thread.Interrupt ()). Again awaittermination () wait for a while, here is the task that is not responding to Thread.Interrupt () mentioned above. Suppose there is no terminated. As soon as prompted executor not finished closed, but also wired to run the task.
void Shutdownandawaittermination (Executorservice pool) { pool.shutdown ();//Disable new tasks from being submitted< c1/>try { //Wait a while for existing the tasks to terminate if (!pool.awaittermination (timeunit.seconds)) { Pool.shutdownnow (); Cancel currently executing tasks //Wait a while for tasks to respond to being cancelled if (!pool.awaittermina tion (timeunit.seconds)) System.err.println ("Pool did not Terminate"); } catch ( Interruptedexception IE) { //(re-) Cancel If current thread also interrupted pool.shutdownnow (); Preserve Interrupt Status thread.currentthread (). interrupt (); }}
In addition to shutting down the executor and checking the executor state, Executorservice also defines a series of submit () and Invoke () to support asynchronous and batch runs of tasks.
The Submit () method supports the callable interface and the normal runnable interface, which is intended to return a future object to support the asynchronous operation of the task. The caller is able to check the running state of the task through the future object. Future-related content will be described in more detail later.
InvokeAll () and Invokeany () provide an interface for running tasks in batches.
Talk about high concurrency (39) parsing java.util.concurrent each component (15) Understanding the design of Executorservice interface