package com.barcap.test.test00;
import java.util.concurrent.*;
public class ExecutorCompletest00 {
public static void main(String[] args) {
ExecutorService exc= Executors.newFixedThreadPool( 10 );
ExecutorCompletionService executorCompletionService= new ExecutorCompletionService( exc );
for (int i=1;i<10;i++){
Task00 task00= new Task00( i );
executorCompletionService.submit( task00 );
}
for (int i=1;i<20;i++){
try {
Future<Integer> future= (Future <Integer>) executorCompletionService.take();
Integer inttest=future.get();
System.out.println(" the result of completion service is "+inttest);
break;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
================================================== =====
package com.barcap.test.test00;
import java.util.*;
import java.util.concurrent.*;
public class ExecutorServ00 {
public static void main(String[] args) {
ExecutorService executorService=Executors.newFixedThreadPool( 9 );
List<Future> futList= new ArrayList <>( );
for (int i=1;i<10;i++) {
Future result= executorService.submit( new Task00( i ) );
futList.add( result );
}
for (Future<Integer> futureEach :futList ){
try {
Integer inm= futureEach.get();
System.out.println("the result of future executorservice is "+inm);
break;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
================================================== =========
package com.barcap.test.test00;
import java.util.concurrent.*;
public class Task00 implements Callable<Integer> {
int i;
public Task00(int i) {
this.i = i;
}
@Override
public Integer call() throws Exception {
System.out.println(" the current thread is "+Thread.currentThread().getName() +" the result should be "+i);
int sleepforsec=100000/i;
Thread.sleep( sleepforsec );
System.out.println(" the task complted for "+Thread.currentThread().getName() +" the result should be "+i);
return i;
}
}
================================================== ====================
执行程序完成服务的日志差异:当前线程是pool-1-thread-1,结果应为1当前线程是pool-1-thread-2,结果应为2当前线程是pool-1-thread-3,结果应为3,当前线程是pool-1-thread-4,结果应该是4,当前线程是pool-1-thread-6,结果应该是6,当前线程是pool-1-thread-5,结果应该是5,当前线程是pool-1-thread-7的结果应为7,当前线程为pool-1-thread-9,结果应为9,当前线程为pool-1-thread-8,其结果应为8。 1线程9的结果应该是9,结果是9池1的任务已编译的任务8线程,结果应该是8池1的任务已编译的任务,结果应该是7任务的压缩pool-1-thread-6的结果应为6的任务pool-1-thread-5的结果应该为5池1线程4的任务的结果应该为4池1线程3的任务应该是4的任务的结果
为pool-1-thread-2编译的任务,结果应为2
当前线程是pool-1-thread-1,结果应为1当前线程是pool-1-thread-3,结果应为3当前线程是pool-1-thread-2,结果应为2,当前线程是pool-1-thread-5,结果应该是5当前线程是pool-1-thread-4,结果应该是4当前线程是pool-1-thread-6,结果应该是6当前线程是pool-1-thread-7的结果应为7,当前线程为pool-1-thread-8,结果应为8,当前线程为pool-1-thread-9,结果应为9,该任务已为pool- 1-thread-9结果应为9池1的任务已编译的线程8线程结果应为8池1线程的任务已编译的结果-thread-7结果应为7池1的任务完成的任务线程6的结果应该是6为pool-1-线程5编译的任务的结果应该是5为pool-1-thread-4压缩的任务的结果应该为4任务对于pool-1-thread-3压缩的任务的结果应该是3对于pool-1-thread-2的任务压缩的结果是2为pool-1-thread-1编译的任务的结果应为1将来的结果为1
================================================== =====
对于executorservice来说,只有在所有任务都经过编译之后,结果才能显示出来。
执行程序完成服务可返回任何结果。