Java thread pool, getting the return value

Source: Internet
Author: User

The callable interface is used in the program to get the return value of the thread.

 PackageLiuzh;Importjava.util.LinkedList;Importjava.util.List;Importjava.util.concurrent.Callable;Importjava.util.concurrent.ExecutionException;Importjava.util.concurrent.Future;ImportJava.util.concurrent.LinkedBlockingDeque;ImportJava.util.concurrent.ThreadPoolExecutor;ImportJava.util.concurrent.TimeUnit;classTaskImplementsCallable<string> {    PrivateString filename;  PublicTask (String filename) { This. FileName =filename; } @Override PublicString Call ()throwsException {Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Done" +filename); return"Return value" +filename; }}/*** Create a thread pool, assign 50 tasks, and the maximum concurrency is 5 * After the end of the run, output the end of each thread return * title:threadpoolexecutortest * descript: *@authorLiuzh * @date May 18, 2016 PM 8:16:26*/ Public classThreadpoolexecutortest { Public voidStart ()throwsinterruptedexception, executionexception {/*** Create thread pool with a maximum concurrency of 5 * linkedblockingdeque, which means to perform a task or put into a queue*/Threadpoolexecutor TPE=NewThreadpoolexecutor (5, 10, 0, Timeunit.seconds,NewLinkedblockingdeque<runnable>(),                NewThreadpoolexecutor.callerrunspolicy ()); //The return value of the storage threadlist<future<string>> results =NewLinkedlist<future<string>>();  for(inti = 0; I < 10; i++) {Task Task=NewTask (string.valueof (i)); System.out.println ("Put into thread pool:" +i); //call submit to get the return value of the threadfuture<string> result =Tpe.submit (Task);                    Results.add (result); }        //This function means that new tasks are no longer received.//if not called, Awaittermination will always blockTpe.shutdown (); //1 days, Analog forever waitingSystem.out.println (Tpe.awaittermination (1, timeunit.days)); //Output Results         for(inti = 0; I < 10; i++) {System.out.println (Results.get (i). get ()); }    }     Public Static voidMain (string[] args)throwsinterruptedexception, executionexception {threadpoolexecutortest test=Newthreadpoolexecutortest ();    Test.start (); }}

Sample output:

Put into thread pool: 0
Put into thread pool: 1
Put into thread pool: 2
Put into thread pool: 3
Put into thread pool: 4
Put into thread pool: 5
Put into thread pool: 6
Put into thread pool: 7
Put into thread pool: 8
Put into thread pool: 9
Complete 1
Complete 0
Complete 2
Complete 3
Complete 4
Complete 9
Complete 6
Complete 8
Complete 5
Complete 7
True
return value 0
return value 1
return value 2
return value 3
return value 4
return value 5
return value 6
return value 7
return value 8
return value 9

Reference: http://www.cnblogs.com/vhua/p/5277694.html

Http://www.cnblogs.com/hzmark/archive/2013/01/21/JavaExecutor.html

Java thread pool, getting the 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.