JDK Document Description callable:
Public Interface Callable<v> A task that returns a result and may throw an exception. The implementing person defines a method called call without any parameters. The callable interface is similar to Runnable, and both 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 ways to convert from other common forms to callable classes
The only way to do this is:
CALLV Call () throws -If the result cannot be calculated
JDK documentation describes the future:
Public Interface null As the result of the underlying task.
The main methods are:
Method Summary |
Boolean |
cancel(boolean mayInterruptIfRunning) Attempt to cancel execution of this task |
V |
get() If necessary, wait for the calculation to complete and then get its results. |
V |
get(long timeout, TimeUnit unit) If necessary, wait for the result to be obtained (if the result is available) after the time given for the calculation to complete. |
Boolean |
isCancelled() Returns trueif the task is canceled before it is completed properly. |
Boolean |
isDone() Returns trueif the task is completed. |
Example:
ImportJava.util.Random;Importjava.util.concurrent.Callable;ImportJava.util.concurrent.CompletionService;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorCompletionService;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future; Public classThreadPool2 { Public Static voidMain (string[] args) {test1 (); } Private Static voidtest1 () {Executorservice es= Executors.newfixedthreadpool (2); for(intI=1; i<=5; i++){ Final intTask =i; Future<Integer> future = Es.submit (NewCallable<integer>() {@Override PublicInteger Call ()throwsException {Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Callable Task" "+ Task +" "Run"); return NewRandom (). Nextint (100); } }); System.out.println ("Current Submission" "+ i +" "Task returning results"); Try{System.out.println ("Task" + i + "" Returns the result: "+future.get ()); } Catch(Exception e) {e.printstacktrace (); }} System.out.println ("I am the code behind the For Loop----------"); Es.shutdown (); }}
Operation Result:
Thetask callablethe task "1" Running task"1" returned by the current submission of section "1" Returns the result: theTask callable task"2 " for which the current submission of section "2" returns results. "Run Task"2 "returned results: the current submission of section"3"Returns the result of the task callable task"3"Run Task"3 "returned results: current submissionof section" 4 The results returned by the Task callable task"4" Running task' 4 ' Returns the result: thetask callable task"5 "Run Task"5 "results returned: I am the code behind the For loop ----------
10, Java5 line Cheng return the results of the task of callable and future