JAVA concurrent Programming-return run results (callable and Future) (ix)

Source: Internet
Author: User

Start a thread regardless of whether you use thread or runnable. are not returning the results.

That is, the run () method of the thread and runnable must have no return value.

public void Run () {}

How to resolve:

callable and the Future , one produces results, one gets results.

a simple example . Demo help us understand:


public class Callableandfuture {/** * @param args */public static void main (string[] args) {//single-thread thread pool Executorservice thre Adpool =  executors.newsinglethreadexecutor (); Future<string> =threadpool.submit (New callable<string> () {public String call () throws Exception { Thread.Sleep (+); return "Hello";};}); System.out.println ("Wait for result"), try {System.out.println ("get result" + future.get ()), "Catch" (Interruptedexception e) { E.printstacktrace ();} catch (Exception e) {e.printstacktrace ();}}}

Operation Result:

Waiting for results

Get the results Hello

Usage scenarios:

If there is a very time-consuming return value that needs to be calculated. And this return value is not immediately necessary, then you can use the above way, with a thread to calculate the return value, and the current thread before using this return value can do other operations, wait until the return value to be required, and then through the future!

Description

The result type obtained by the future must be the same as the result type returned by callable, which is implemented by generics .

callable to use Executor Service of the Submit method is submitted, returned as Future Object

Completionservice used to submit a set of callable task, its Take method to return an already completed callable task corresponding to the Future Object

Like I planted a few pieces of wheat at the same time and waited for the harvest. When the harvest is ripe, which piece is first harvested?

let's take the following Completionservice look at a little Demo


public class Callableandfuture {/** * @param args */public static void main (string[] args) {Executorservice threadPool2 =< C1/>executors.newfixedthreadpool (10); completionservice<integer> completionservice = new executorcompletionservice<integer> (threadPool2); (int i=1;i<=10;i++) {final int seq = i;completionservice.submit (new callable<integer> () {@Overridepublic Integer call () throws Exception {thread.sleep (New Random () Nextint (); return seq;}}); for (int i=0;i<10;i++) {try {System.out.println (Completionservice.take (). get ());} catch (Interruptedexception e) { E.printstacktrace ();} catch (Executionexception e) {e.printstacktrace ();}}}}

Operation Result:

8

6

5

7

2

1

3

10

9

4

Summarize:

The need for multi-threaded run return results is very rare in practical applications, and this article is just as an understanding learning.




JAVA concurrent Programming-return run results (callable and Future) (ix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.