rookie talk about--callable and future .
1. What is
The callable interface is similar to runnable. But Runnable does not return the result, and cannot throw an exception, and the callable function is more powerful, after the thread executes, you can return the value, which can be future, that is, future can get the return value of the asynchronous execution task.
The code sample is as follows:
Package cn.km.callableAndFuture;
Import Java.util.Random;
Import java.util.concurrent.Callable;
Import Java.util.concurrent.FutureTask;
public class Test {public
static void Main (string[] args) {
callable<integer> callable = new Callable<in Teger> () {
@Override public
Integer call () throws Exception {return
new Random (). Nextint ();
};
futuretask<integer> futuretask = new futuretask<> (callable);
New Thread (Futuretask). Start (); Open the thread to execute
try {
thread.sleep (),//do something
System.out.println (Futuretask.get ());//get returned result value
catch (Exception e) {
e.printstacktrace ();}
}
}
2. Principle
Futuretask implements two interfaces runnable and future, so it can be executed as a runnable by the thread, and can be future to get callable return value ...
Suppose there is a time-consuming return value that is not computed, and this return value is not immediately required, then you can use another thread to compute the return value, and when the front line can do something else before using the return value, wait until the return value is needed, and then get it via future. 2.1 Future Interface class parsing
Public interface Future<v> {
boolean mayinterruptifrunning);
Boolean iscancelled ();
Boolean isdone ();
V get () throws Interruptedexception, Executionexception;
V get (long timeout, timeunit)
throws Interruptedexception, Executionexception, timeoutexception;
}
Parse the role of each method:
Cancel method: used to cancel a task, return TRUE if the cancellation succeeds, or False if the cancel task fails.
Parameter mayinterruptifrunning indicates whether to allow cancellation of a task that is executing but not completed, and if set true, indicates that the task in progress can be canceled.
If the task has been completed, whether mayinterruptifrunning is true or FALSE, this method will definitely return false, that is, canceling the completed task cancel () returns false.
If the task is executing, if mayinterruptifrunning is set to True, the Cancel () function returns True, and if Mayinterruptifrunning is set to False, the Cancel () function returns FALSE.
If the task has not yet been executed, it will definitely return true, regardless of whether mayinterruptifrunning is set to True or false.
iscancelled () method: If the task was canceled successfully, return True if it was canceled before the task was properly completed.
Isdone () method: If the task is completed, return TRUE if the task completes.
Get () method: used to obtain execution results, this method will cause blocking and will wait until the task is completed before returning.
Get (a long Timeout,timeunit unit) method: To obtain the execution result, if the result is not obtained within the specified time range, return null directly.
* * Summary: **future provides three functions: 1 to determine whether the task is completed, 2 to interrupt the task, 3 to obtain the task execution results. 2.2 Futuretask class parsing
the realization of Futuretask
the realization of runnablefuture
Public interface runnablefuture<v> extends Runnable, future<v>
From the above, Futuretask can be called as runnable by the thread, and can be used as future to get callable return value.
Futuretask Constructors
Public Futuretask (callable<v> callable) {
if (callable = = null)
throw new NullPointerException ();
this.callable = callable;
This.state = NEW; Ensure visibility of callable
} public
Futuretask (Runnable Runnable, V result) {
this.callable = executors . Callable (runnable, result);
This.state = NEW; Ensure visibility of callable
}