Executorservice and Executorcompletionservice are java.util.concurrent packets of concurrent processing classes, in general, Executorcompletionservice is the function of Executorservice Strong version, Executorcompletionservice to blockingqueue<future<v>> to store the completed tasks.
In other words, priority tasks are prioritized in the blockingqueue<future<v>> queue, so we get the highest-priority results in a timely manner.
Let's take a look at Executorservice's test code, a total of 4 tasks, we deliberately let the 1th task of the longest execution time, descending, the code is as follows:
import java.util.concurrent.callable;import java.util.concurrent.executionexception;import java.util.concurrent.executorservice;import java.util.concurrent.executors;import Java.util.concurrent.future;public class testexecutorservice {public static void main (String[] args) throws interruptedexception, executionexception { Executorservice es = executors.newfixedthreadpool (4);//First task Callable<integer> task1 = new Callable<Integer> () {public integer call () throws exception {//takes the longest time, 4 seconds Thread.Sleep (4000); return 1;}};/ /second Task callable<integer> task2 = new callable<integer> () {public Integer call () throws exception {thread.sleep (+); return 2;}};/ /A third task callable<integer> task3 = new callable<integer> () {public Integer caLL () throws exception {thread.sleep (+); return 3;}};/ /Fourth Task callable<integer> task4 = new callable<integer> () {public Integer call () throws exception {thread.sleep (+);return 4;}}; Future<integer> result1 = es.submit (TASK1); Future<integer> result2 = es.submit (TASK2); Future<integer> result3 = es.submit (TASK3); Future<integer> result4 = es.submit (TASK4); SYSTEM.OUT.PRINTLN ("1th task Waiting ..."); SYSTEM.OUT.PRINTLN ("1th Task completed:" " + result1.get () + " ""); SYSTEM.OUT.PRINTLN ("2nd task Waiting ..."); SYSTEM.OUT.PRINTLN ("2nd Task completed:" " + result2.get () + " ""); SYSTEM.OUT.PRINTLN ("3rd task Waiting ..."); SYSTEM.OUT.PRINTLN ("3rd Task Completed:" " + result3.get () + " ""); SYSTEM.OUT.PRINTLN ("4th task Waiting ..."); SYSTEM.OUT.PRINTLN ("4th Task completed:" " + result4.get () + " ");}}
Output Result:
1th Task Waiting ... 1th task completed: "1" 2nd task waiting ... 2nd task completed: "2" 3rd task waiting ... 3rd Task completed: "3" 4th task waiting ... Completion of the 4th task: "4"
The PS:Future.get () method causes blocking until the task finishes executing.
The running code is visible, Result1.get () blocks 4 seconds to complete the task, the output results, and immediately after the Result2.get (), Result3.get (), Result4.get () does not block, and immediately outputs the result. That's because when the Result1.get () execution is complete, the remaining 3 tasks are already executed and waiting for the results to be fetched. Therefore, using the above method does not know which task is the first to return the result.
Next, let's look at the code for Executorcompletionservice:
import java.util.concurrent.callable;import java.util.concurrent.executionexception;import java.util.concurrent.executorcompletionservice;import java.util.concurrent.executorservice;import java.util.concurrent.executors;import java.util.concurrent.future;public class Testexecutorcomplectionservice {public static void main (String[] args) throws InterruptedException, ExecutionException {ExecutorService es = Executors.newfixedthreadpool (4); Executorcompletionservice<integer> ecs = new executorcompletionservice<integer > (es);//First task callable<integer> task1 = new callable<integer> () {public integer call () throws exception {//takes the longest time, 4 seconds Thread.Sleep (4000); return 1;}};/ /second Task callable<integer> task2 = new callable<integer> () {public Integer call () Throws exception {thread.sleep (return 2;}};/); /A third task callable<integer> task3 = new callable<integer> () {public Integer call () throws exception {thread.sleep (return 3;}};/); /Fourth Task callable<integer> task4 = new callable<integer> () {public Integer call () throws exception {thread.sleep (+);return 4;}}; Ecs.submit (TASK1); Ecs.submit (TASK2); Ecs.submit (TASK3); Ecs.submit (TASK4); for (int i = 0; i < 4; i++) {future<integer> result = ecs.take (); SYSTEM.OUT.PRINTLN ("Output result:" " + result.get () + " "");}}}
Output Result:
Output: "4" Output: "3" Output Result: "2" Output: "1"
As can be seen, the 4th task Task4 execution time is the shortest, 1th output result.
Let us analyze the source of Executorcomplectionservice:
The member variables are as follows:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/82/BC/wKioL1dfvU6wWyrxAAAv-TgEQAc139.jpg-wh_500x0-wm_3 -wmp_4-s_1834063374.jpg "title=" qq20160614161008.jpg "alt=" Wkiol1dfvu6wwyrxaaav-tgeqac139.jpg-wh_50 "/>
Executor:executorservice class, Task parallel executor
Completionqueue: Is the blocking queue that holds execution results blockingqueue
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/82/BC/wKioL1dfvpiyDtmzAAAnFl_RQXU002.png-wh_500x0-wm_3 -wmp_4-s_2868982788.png "title=" qq picture 20160614161523.png "alt=" Wkiol1dfvpiydtmzaaanfl_rqxu002.png-wh_50 "/>
Submit method: The bottom layer still uses Executorservice to execute the task concurrently, but it is more than one function "put the completed task into the Complectionqueue queue"
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/BE/wKiom1dfvlvAmAxeAAAaV7kvm6U428.jpg-wh_500x0-wm_3 -wmp_4-s_4028195785.jpg "title=" qq20160614161847.jpg "alt=" Wkiom1dfvlvamaxeaaaav7kvm6u428.jpg-wh_50 "/>
Task method: Gets an element from the Complectionqueue queue and, if there is no element, blocks until there is an element position in the queue, which validates our previous assertion.
Summary: Overall, Executorcomplectionservice is actually the combination of Executorservice and blockingqueue.
This article is from the "Deaconli" blog, make sure to keep this source http://lizhuquan0769.blog.51cto.com/2591147/1789158
"Java multithreading" Executorservice and Executorcompletionservice