Then on a continuation of the contract and the study, this article describes the callable and future, they are very interesting, one produces results, one to get results.
Callable interface is similar to runnable, can be seen from the name, but Runnable will not return results, and can not be thrown to return the result of the exception, and callable more powerful, after being executed by the thread can return a value, this return value can be obtained by the future , that is to say, the future can get the return value of the asynchronous execution task, here is a simple example:
Public classCallableandfuture { Public Static voidMain (string[] args) {callable<Integer> callable =NewCallable<integer>() { PublicInteger Call ()throwsException {return NewRandom (). Nextint (100); } }; Futuretask<Integer> future =NewFuturetask<integer>(callable); NewThread (Future). Start (); Try{Thread.Sleep (5000);//might do somethingSystem.out.println (Future.get ()); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(executionexception e) {e.printstacktrace (); } }}
Futuretask implements two interfaces, runnable and future, so it can be executed as a runnable thread, and can be used as the future to get callable return value, so what is the benefit of using this combination? Assuming that there is a time-consuming return value that needs to be computed, and that the return value is not immediately required, then you can use this combination to compute the return value with another thread, and the current thread can do other things before using the return value, and when this return value is needed, then the future is not beautiful! Here is an introduction to the future model: Http://openhome.cc/Gossip/DesignPattern/FuturePattern.htm.
Here's another way to use callable and the future, execute callable through the Executorservice submit method, and return to the future with the following code:
Public classCallableandfuture { Public Static voidMain (string[] args) {Executorservice ThreadPool=Executors.newsinglethreadexecutor (); Future<Integer> future = Threadpool.submit (NewCallable<integer>() { PublicInteger Call ()throwsException {return NewRandom (). Nextint (100); } }); Try{Thread.Sleep (5000);//might do somethingSystem.out.println (Future.get ()); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(executionexception e) {e.printstacktrace (); } }}
The code is not a lot simpler, Executorservice inherits from executor, its purpose is to manage thread objects for us, thus simplifying concurrent programming, executor we do not need to display to manage the life cycle of threads, is the preferred way to launch tasks after JDK 5.
Execute multiple tasks with return values and get multiple return values, with the following code:
Public classCallableandfuture { Public Static voidMain (string[] args) {Executorservice ThreadPool=Executors.newcachedthreadpool (); Completionservice<Integer> cs =NewExecutorcompletionservice<integer>(ThreadPool); for(inti = 1; I < 5; i++) { Final intTaskID =i; Cs.submit (NewCallable<integer>() { PublicInteger Call ()throwsException {returnTaskID; } }); } //might do something for(inti = 1; I < 5; i++) { Try{System.out.println (Cs.take (). get ()); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(executionexception e) {e.printstacktrace (); } } }}
In fact, you can not use Completionservice, you can first create a set of future types, with executor submitted task return value added to the collection, and finally iterate through the collection of data, code slightly.
This article from: Gao | Coder, the original address: http://blog.csdn.net/ghsau/article/details/7451464, reprint please specify.
Java Threads (vii): Callable and future