Java multi-thread ~~~callable interface Get return value

Source: Internet
Author: User

Threadpoolexecutor provides another very powerful interface, and that is callable. This interface is similar to runnable, but implementing this

The method of the interface is the call method, which can return a value, making up the sorrow that runnable cannot return a value. And this method can be used in conjunction with THREADP

Oolexecutor use, get the future interface, from the name of this interface we can know that the return of this is similar to pointing to this thread of a pointer, I

We can know the current thread's operation through this future interface, including whether the task has been completed, the current running condition, the return value after the completion of the run, etc.

And each task is created directly through Threadpoolexecutor's Submit method, with a very automated feel. I can't stop it.

Get down. Let's illustrate the use of this interface with the example of calculating multiple numbers.


Package Com.bird.concursey.charpet6;import Java.util.arraylist;import Java.util.list;import java.util.Random; Import Java.util.concurrent.callable;import Java.util.concurrent.executionexception;import Java.util.concurrent.executors;import Java.util.concurrent.future;import Java.util.concurrent.ThreadPoolExecutor ; Import Java.util.concurrent.timeunit;public class Factorialcalculator implements callable<integer> {private int number;public factorialcalculator (int number) {super (); this.number = number;} @Overridepublic Integer Call () throws Exception {int result = 1;if (number = = 0 | | number = = 1) {result = 1;} else{for (int i = 2; I <= number; i++) {result *= i; TimeUnit.MILLISECONDS.sleep (20);}} System.out.printf ("%s:%d\n", Thread.CurrentThread (). GetName (), result), return result; public static void Main (string[] args) {Threadpoolexecutor execute = (threadpoolexecutor) Executors.newfixedthreadpool ( 3); list<future<integer>> results = new arraylist<future<integer>> ();Random random = new random (), for (int i = 0; i < i++) {int number = Random.nextint (20); Factorialcalculator CALCU = new Factorialcalculator (number); future<integer> result = Execute.submit (CALCU); Results.add (result);} do{system.out.printf ("Main:number of Completed tasks:%d\n", Execute.getcompletedtaskcount ()); for (int i=0; i< Results.size (); i++) {future<integer> result=results.get (i); System.out.printf ("Main:task%d:%s\n", I,result.isdone ());} try {TimeUnit.MILLISECONDS.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} while (Execute.getcompletedtaskcount () < results.size ()); System.out.printf ("main:results\n"); for (future<integer> fus:results) {try {System.out.printf ("Main:task%d:% D\n ", 1,fus.get ());} catch (Interruptedexception e) {e.printstacktrace ();} catch (Executionexception e) {e.printstacktrace ()}} Execute.shutdown ();}}

Remember that when you are done, you must remember shutdown, this method, if you do not call this method, execute will wait for the run, even if no thread

Is running, Shundown does not immediately shut down all the threads, he is just the table name, and when the other threads he controls are finished, execute automatically shuts down

Only.

Java multi-thread ~~~callable interface Get 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.