FutureTask class, futuretask
The FutureTask class is an implementation of Future and implements Runnable,Therefore, it can be executed through Executor (Thread pool) or passed to the Thread object for execution.If you want to perform time-consuming operations in the main thread but do not want to block the main thread,These jobs can be handed over to the Future object to be completed in the background. When the main thread needs it in the Future,You can use the Future object to obtain the computing result or execution status of the background job.The Executor framework uses FutureTask to complete asynchronous tasks and can be used for any potentially time-consuming computing. Generally, FutureTask is mostly used for time-consuming computing. The main thread can obtain the result after completing its own task.
/*** Created with IntelliJ IDEA. * User: cainiao Daming * Date: 14-10-23 * Time: * To change this template use File | Settings | File Templates. */public class MyCallable1 implements Callable {@ Override public Object call () throws Exception {System. out. println ("call"); return "end";} public static void main (String [] args) throws ExecutionException, InterruptedException {MyCallable1 myCallable1 = New MyCallable1 (); FutureTask fk = new MyFutureTask (myCallable1); // It can be directly executed through Thread packaging, // Thread thread = new Thread (fk); // thread. start (); // You can also submit it to ExecuteService for execution. // ExecutorService exec = Executors. newCachedThreadPool (); // Future <String> future = exec. submit (myCallable1); // you can also use v get () to return the execution result. // when the thread body is not completed, the main thread waits for blocking, after the execution, the result is directly returned. Fk. run (); System. out. println (fk. get () ;}} class MyFutureTask extends FutureTask {public MyFutureTask (Callable callable) {super (callable) ;}// when the thread stops running, run the done method. @ Override public void done () {// This is generally used to calculate the time consumed for parsing tasks. System. out. println ("done ");}}
FutureTask-like usage?
This class can cancel asynchronous computing. This class provides the basic implementation of Future by using the calculation start and cancel methods, the method for querying whether the calculation is complete, and the method for retrieving the calculation result. Results can be retrieved only when the calculation is complete. If the calculation is not completed, the get method is blocked. Once the calculation is complete, you cannot start or cancel the calculation again.
You can use FutureTask to wrap Callable or Runnable objects. Because FutureTask implements Runnable, you can submit FutureTask to Executor for execution.
In addition to being an independent class, this class also provides the protected function, which may be useful when creating a custom task class.
How does Java implement method blocking?
If you want to use a thread for implementation, at least two threads are required. If one thread is stopped, the other thread is manipulated, and then all threads are awakened through the other thread to continue execution.