package com.barcap.test.test00;
import java.util.concurrent.*;
/**
* Created by Sony on 25-04-2019.
*/
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.*;
/**
* Created by Sony on 25-04-2019.
*/
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.*;
/**
* Created by Sony on 25-04-2019.
*/
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
为 pool-1-thread-9 完成的任务结果应该是 9
结果是 9
为 pool-1-thread-8 完成的任务结果应该是 8
为 pool-1-thread-7 完成的任务结果应该是 7
为 pool-1-thread-6 完成的任务结果应该是 6
为 pool-1-thread-5 完成的任务结果应该是 5
为 pool-1-thread-4 完成的任务结果应该是 4
为 pool-1-thread-3 完成的任务结果应该是 3
为 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
为 pool-1-thread-8 完成的任务结果应该是 8
为 pool-1-thread-7 完成的任务结果应该是 7
为 pool-1-thread-6 完成的任务结果应该是 6
为 pool-1-thread-5 完成的任务结果应该是 5
为 pool-1-thread-4 完成的任务结果应该是 4
为 pool-1-thread-3 完成的任务结果应该是 3
为 pool-1-thread-2 完成的任务结果应该是 2
为 pool-1-thread-1 完成的任务结果应该是 1
未来的结果是1
================================================ ========
对于 executorservice,只有在所有任务完成后才能获得结果。
executor completionservice 任何可用的结果都会返回。