Talk about high concurrency (39) parsing java.util.concurrent each component (15) Understanding the design of Executorservice interface

Source: Internet
Author: User

The previous article spoke about the design of the executor interface, which was designed to decouple the execution of tasks from the submission of tasks and to hide the execution strategy of the task. This article says Executorservice interface. It expands the executor interface, manages the life cycle of the executor, and extends it further.


Executor is responsible for performing the task. It has a life cycle of 3: Running, shutting down, and terminating. At any point in time, some tasks may have been completed, some may be running, and some may be waiting to be executed in the queue. So if you want to turn off executor, there are a number of ways, such as graceful smooth closing, when execution is closed, you are not accepting a new task request, and wait until the running task has finished executing and then close executor; For example, the most brutal shutdown, direct shutdown executor, Regardless of whether the running task is complete.

1. Shutdown () method is graceful to close the way

2. Shutdownnow () is a rude shutdown, which returns a list of tasks that have not yet begun to execute. For a running task, it uses the Thread.Interrupt () method to cancel the task. If the task does not respond to a thread's interruption, the task may continue to execute.

3. The Awaittermination () method waits for a period of time to terminate the execution of the 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 Interru        Ptedexception;                                  <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, Executionexception, Timeoutexcepti On;}

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 complete, and then Shutdownnow () forcibly close (in the Way of Thread.Interrupt ()). Again awaittermination () wait for a while, here is the task that is not responding to Thread.Interrupt () mentioned above. If you do not have a terminated, you are prompted executor not complete the shutdown, but also the thread is performing 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 executor and checking the executor state, Executorservice also defines a series of submit () and Invoke () to support task asynchronous execution and batch execution.

The Submit () method supports the callable interface and the normal runnable interface, which is intended to return a future object to support asynchronous execution of the task. The caller can use the future object to check the execution status of the task. Future-related content will be described in detail later.

InvokeAll () and Invokeany () provide an interface for bulk execution of tasks.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk about high concurrency (39) parsing java.util.concurrent each component (15) Understanding the design of Executorservice interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.