From the school to A factory all the way sunshine vicissitudes of life

Please stampwww.codercc.com

1. FutureTask profile

FutureTask is used to represent an asynchronous task that can obtain results in the Executors framework. FutureTask implements the Future interface. FutureTask provides common methods for starting and canceling asynchronous tasks, querying whether an asynchronous task evaluates to completion, and getting the result of the final asynchronous task. The get() method is used to get the results of the asynchronous task, but blocks the current thread until the asynchronous task is finished. Once task execution is complete, the task cannot be restarted or cancelled unless the runAndReset() method is called. The FutureTask source code defines these states for it:

private static final int NEW          = 0;
private static final int COMPLETING   = 1;
private static final int NORMAL       = 2;
private static final int EXCEPTIONAL  = 3;
private static final int CANCELLED    = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED  = 6;
Copy the code

In addition, in the Art of Concurrent Programming in Java, FutureTask is divided into three states according to the timing of the execution of the futureTask.run () method:

  1. Don’t start. FutureTask is not started until the futureTask.run () method is executed. When a FutureTask is created, it is not started until the futureTask.run () method is executed.
  2. Is started. The futureTask.run () method is executed while FutureTask is started.
  3. Has been completed. The futureTask.run () method ends, or futureTask.cancel (…) is called. Method cancels the task, or throws an exception during the execution of the task, which is called the completed state of FutureTask.

The following figure summarizes the state change process for FutureTask:

FutureTask state migration diagram. JPG

Since FutureTask has these three states, the results of executing FutureTask’s GET and cancel methods are quite different depending on the current state. Here’s a summary of the GET and cancel methods:

The get method

When FutureTask is not started or started, executing the futureTask.get () method will cause the calling thread to block. If FutureTask is in the completed state, calling the futureTask.get () method causes the calling thread to immediately return the result or throw an exception

Cancel method

When FutureTask is not started, the futureTask.cancel () method will never execute the task;

Executing futureTask.cancel (true) while FutureTask is started prevents the task from continuing by interrupting the thread. Executing futureTask.Cancel (false) has no effect on the thread executing the task.

When FutureTask is completed, futureTask.cancel (…) is executed. Method will return false.

The Future’s get() and cancel() methods are summarized in the following figure

Schematic of FutureTask’s get and cancel execution. JPG

2. Basic use of FutureTask

FutureTask implements the Runnable interface in addition to the Future interface. Thus, FutureTask can be handed to executors or executed directly by the calling thread (futureTask.run ()). Alternatively, FutureTask retrieval can return a FutureTask object through the executorService.submit () method, and then through the FutureTask.get() or futureTask.cancel method.

** FutureTask is used when a thread needs to wait for another thread to complete a task before it can continue. If there are multiple threads performing several tasks, each task can be executed at most once. When multiple threads attempt to execute the same task, only one thread is allowed to execute the task, and the other threads must wait for the task to complete before continuing to execute.

reference

The Art of Concurrent Programming in Java