callable--creating a thread with a return value

Source: Internet
Author: User

Turn from: JAVA notes callable and Futuretask: Multithreading with return values

The commonly used thread class has no return value after the Run method executes, and a third-party dump is required to implement the return value to the main thread after the child thread completes the task. The callable interface provides a multithreaded implementation method with return values. Here is a simple landlord, Overwatch Hachang example to show the use of this interface.

Permanent class:

The working class implements the callable interface, which returns an integer value after the thread has finished running.

 Packagecom.lk.c;Importjava.util.concurrent.Callable; Public classChanggongImplementsCallable<integer>{//generics restrict the return value type of an overriding method    Private intHours=12; Private intamount; @Override PublicInteger Call ()throwsException { while(hours>0) {System.out.println ("I ' m working ..."); Amount++; Hours--; Thread.Sleep (1000); }        returnamount; }}

Landlord: Master Process

Supervisor: Futuretask

 Packagecom.lk.c;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.FutureTask; Public classDizhu { Public Static voidMain (String args[]) {Changgong worker=NewChanggong (); Futuretask<Integer> Jiangong =NewFuturetask<integer>(worker); NewThread (Jiangong). Start ();  while(!Jiangong.isdone ()) {            Try{System.out.println ("The View from the ..."); Thread.Sleep (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        intamount; Try{Amount=Jiangong.get (); System.out.println ("Finished the work, turned in" +amount); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(executionexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    }}
I ' m working ... See if the permanent has been finished ... I' m working ... See if the permanent has been finished ... See if the permanent has been finished ... I' m working ... See if the permanent has been finished ... I' m working ... I ' m working ... See if the permanent has been finished ... I' m working ... See if the permanent has been finished ... See if the permanent has been finished ... I' m working ... I ' m working ... See if the permanent has been finished ... See if the permanent has been finished ... I' m working ... I ' m working ... See if the permanent has been finished ... I' m working ... See if the permanent has been finished ... See if the permanent has been finished ... I' m working ... See if the permanent has been finished ... Finished the work and handed in

callable--creating a thread with a return value

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.