One: Future
When creating a thread with implementation callable, the call () method has a return value. So what do we use in programming to represent future results that the thread executes before it can be returned ? That's the future type.
As the name implies, future--future values, we use this future value in place of the thread results in the programming, and then in the actual run, through the Future.get () method to get the actual results of the thread.
The future interface has a generic parameter whose type is the same as the return value type of the call () method, meaning that,future<v> only represents the next value of the V type, not the existence of a value, and the need to use this specific value. A V-type object will be required to get the actual running result value of the thread through get ().
Two: Futuretask
Futuretask is an asynchronous thread that can be canceled (canceled before the completion of the thread execution) , which implements the Runnable interface and the future interface, which means that it can run both threads and return results. Therefore: the thread class that implements runnable creation, the thread class that implements the callable implementation, can be wrapped into Futuretask object to start.
Futuretask implements the future interface, so the Get (), IsDone (), and Cancel () methods in the future are all implemented, so the execution result of the asynchronous thread can be obtained through the Futuretask object, the completion state of the query thread, and the thread is canceled.
Futuretask implements the Runable interface, so it can be wrapped as the thread class's construction parameters as thread classes to start, or it can be submitted to the thread pool's Excute (Futuretask) to start.
Use procedure: Create Runnable implementation Class object or callable implement class object--Create Futuretask object, take Runnable implementation class object or callable implement class object as parameter
--Create the thread object to take the Futuretask object as a parameter, and then pass the thread object. Start (), save the asynchronous result with the list of future<v> types
--or create a thread pool, give the Futuretask object as a parameter to Excute (Futuretask) to start, and save the asynchronous result by a list of future<v> types
---After the thread's Isdone () query thread to complete the state, cancel the thread through cancel () and get the real result through the get () of the future result. ( if Cancel () is called in the Get () thread, an exception is thrown )
Java multithreading future and Futuretask