In the previous article we learned about thread management for the executor framework, this article will learn about another feature of the executor framework, and we know that performing runnable tasks is not a worthwhile return, but executor can run concurrent tasks and get return values, The concurrent package provides the following two interfaces for this function:
Callable interface: This interface declares call (), similar to Runnable's Run (), which allows you to implement specific logical operations of the task in this method. Callable is a generic interface and must declare the return type of call ().
Future interface: This interface declares a way to get the return data of the callable.
How to use
The submit () method will return to the future object by submitting the callable task execution via Executor's submit (). With future objects you can:
Use the Isdone () method to see if the task is complete.
The return value executed by the call () method is obtained by the Get () method, which waits until the callable object executes the call () method and returns the result. If an interrupt occurs, the get () method throws Interruptedexception, or the exception that is thrown by the call () method.
PackageMyThread;ImportJava.util.ArrayList;ImportJava.util.Random;Importjava.util.concurrent.Callable;ImportJava.util.concurrent.ExecutionException;ImportJava.util.concurrent.Executors;ImportJava.util.concurrent.Future;ImportJava.util.concurrent.ThreadPoolExecutor; Public class executortest { Public Static void Main(string[] args) {Threadpoolexecutor executor = (threadpoolexecutor) executors.newcachedthreadpool (); Arraylist<future<string>> list=NewArraylist<> (); for(inti =0; I <5; i++) {future<string> Result=executor.submit (NewTask ()); List.add (result); } for(future<string> f:list) {Try{//while (!f.isdone ()) {};System.out.println (F.get ()); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(Executionexception e) {//TODO auto-generated catch blockE.printstacktrace (); }} executor.shutdown (); }}class Task implements callable<string>{@Override PublicStringPager()throwsException {//TODO auto-generated method stubRandom random=NewRandom ();intResult=random.nextint ( -);//Second stage--waiting for all players to be ready.Thread.Sleep ((Long) (Random.nextint (5) * +));returnThread.CurrentThread (). GetName () +":"+result; }}
Results:
pool-1-thread-1:84
pool-1-thread-2:78
Pool-1-thread-3:15
pool-1-thread-4:99
Pool-1-thread-5:2
run multiple tasks and process the results:
Threadpoolexecutor enables multiple callable tasks to be put into the collection through a InvokeAll () method, and waits for all tasks to complete before returning the results to the list of future links.
unlike submit (), InvokeAll () blocks until the task is completed.
For example:
PackageMyThread;ImportJava.util.ArrayList;ImportJava.util.List;ImportJava.util.Random;Importjava.util.concurrent.Callable;ImportJava.util.concurrent.ExecutionException;ImportJava.util.concurrent.Executors;ImportJava.util.concurrent.Future;ImportJava.util.concurrent.ThreadPoolExecutor; Public class executortest { Public Static void Main(string[] args) {Threadpoolexecutor executor = (threadpoolexecutor) executors.newcachedthreadpool ();//Task linked listList<task> tasklist=NewArraylist<> ();//Results linked listList<future<string>> resultlist=NewArraylist<> (); for(inti =0; I <5; i++) {Tasklist.add (NewTask ()); }Try{Resultlist=executor.invokeall (tasklist); }Catch(Interruptedexception E1) {//TODO auto-generated catch blockE1.printstacktrace (); } System.out.println ("Get execution Result:"); for(future<string> f:resultlist) {Try{//while (!f.isdone ()) {}; System.out.println (F.get ()); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(Executionexception e) {//TODO auto-generated catch blockE.printstacktrace (); }} executor.shutdown (); }}class Task implements callable<string>{@Override PublicStringPager()throwsException {//TODO auto-generated method stubRandom random=NewRandom ();intResult=random.nextint ( -);//Second stage--waiting for all players to be ready.Thread.Sleep ((Long) (Random.nextint (5) * +));returnThread.CurrentThread (). GetName () +":"+result; }}
Java Concurrency Programming-executor framework callable and future interfaces