1, callable:
Callable<v>
The task that returns the result and may throw an exception. The implementing person defines a method called call without any arguments.
The callable interface is similar to Runnable, both of which are designed for classes whose instances might be executed by another thread. However, Runnable does not return a result and cannot throw a checked exception.
The Executors class contains some practical methods for converting from other common forms into callable classes. Call
Call ()
throws Exception
Evaluates the result and throws an exception if the result cannot be computed.
return: The result of the calculation is thrown: Exception-If the result cannot be computed 2, Future:
Future<v>
Future represents the result of an asynchronous computation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to get the result of the calculation. You can only use the Get method to obtain the results after the calculation is complete, and if necessary, block this method before the calculation completes. Cancellation is performed by the Cancel method. Other methods are provided to determine whether the task was completed normally or was canceled. Once the calculation is complete, the calculation can no longer be canceled. If you use Future for the sake of cancellation but do not provide the available results, you can declare the Future<?> form type and return null as the result of the underlying task. Get
Get ()
throws Interruptedexception,
executionexception
If necessary, wait for the calculation to complete, and then obtain its results.
returns: The computed result is thrown: cancellationexception-If the calculation is canceled executionexception-if the calculation throws an exception interruptedexception-if the current The thread was interrupted while waiting 3, related instance:
<pre name= "code" class= "Java" >public class Testfuturetask {public
static void Main (string[] args) throws Inter Ruptedexception, executionexception {
final executorservice exec = Executors.newfixedthreadpool (5);
Callable<string> call = new Callable<string> () {public
String call () throws Exception {
Thread.Sleep (1000 * 3);//Hibernate specified time, where this indicates that the operation is more time-consuming to return "the other
less important but longtime things.";
future<string> task = Exec.submit (call);
Thread.Sleep (1000 * 3);
SYSTEM.OUT.PRINTLN ("Let's do important things.");
A
Boolean isdone = Task.isdone ()
is blocked if something inside the thread has not been processed yet, that is, the result has not yet been returned. while (!isdone) {
isdone = Task.isdone ();
}
String obj = Task.get ();
System.out.println (obj);
Close the thread pool
exec.shutdown ();
}
Output results:
Let ' s do important things.
Other less important but longtime things.
Http://www.itzhai.com/callable-and-future-realization-of-the-thread-waiting-for.html
Other
public class TestFutureTask2 {public static void main (string[] args) throws Interruptedexception, Executionexception {
Final Executorservice exec = Executors.newfixedthreadpool (5);
Runnable Runnable = new Runnable () {@Override public void run () {try {thread.sleep (1000 * 2);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
}//the time specified by hibernation, which indicates that the operation is more time-consuming}};
future<string> task = Exec.submit (runnable, "AA");
SYSTEM.OUT.PRINTLN ("Let's do important things.");
A Boolean isdone = Task.isdone () is blocked if something inside the thread has not been processed yet, that is, the result has not yet been returned. while (!isdone) {//causes the currently executing thread to sleep (temporarily cease execution) for the specified number
of milliseconds, Thread.Sleep (500);
Isdone = Task.isdone ();
The String obj = Task.get ();
System.out.println (obj);//output AA, that is, the parameter passed by the above Submit method//Close the thread pool Exec.shutdown ();
}
}
public class TestFutureTask3 {public static void main (string[] args) throws Interruptedexception, Executionexception {
Final Executorservice exec = Executors.newfixedthreadpool (5);
Runnable Runnable = new Runnable () {@Override public void run () {try {thread.sleep (1000 * 2);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
}//the time specified by hibernation, which indicates that the operation is more time-consuming}};
future<?> task = Exec.submit (runnable);
SYSTEM.OUT.PRINTLN ("Let's do important things.");
Unimportant thing//If the things inside the thread are not finished, that is, the result is not returned, then the Boolean isdone = Task.isdone () is blocked; while (!isdone) {//causes the currently executing thread to sleep (temporarily cease execution) for the specified number
of milliseconds, Thread.Sleep (500);
Isdone = Task.isdone ();
The String obj = (string) task.get ();
System.out.println (obj);//output NULL//Close thread pool Exec.shutdown (); }
}