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; } }
=================================================== =====================
difference in the logs for the executor termination service: the current thread is pool-1-thread-1, the result should be 1, the current thread is pool-1-thread-2, the result should be 2, the current thread is pool-1-thread - 3 result should be 3 current thread - pool-1-thread-4 result should be 4 current thread - pool-1-thread-6 result should be 6 current thread - pool-1-thread-5 result should be 5 current thread is pool-1-thread-7 the result should be 7 the current thread is pool-1-thread-9 the result should be 9 the current thread is pool-1-thread-8 the result should If 8 is back ca executed for a pool-1-thread 9, then the result should be 9, and the result - 9 - The task is made for a pool-1-thread-8. The result should be 8 - The task was completed for pool-1-thread-7. the result should be 7 task completed for pool-1-thread-6 result should be 6 task completed for pool-1-thread-5 result should be 5 task completed for pool-1-thread-4 result should be 4 task completed for pool -1-thread-3 result should be 3
task completed for pool-1-thread-2 result should be 2
the current thread is pool-1-thread-1, the result should be 1, the current thread is pool-1-thread-3, the result should be 3, the current thread is pool-1-thread-2, the result should be 2 current thread this pool-1-thread-5 result should be 5 current thread this pool-1-thread-4 result should be 4 current thread this pool-1-thread-6 result should be 6 current thread pool-1-thread-7 result should be 7 current thread - pool-1-thread-8 result should be 8 current thread - pool-1-thread-9 result should be 9 task completed for pool- 1-thread-9 result should be 9 task completed for pool -1-thread-8 result t should be 8 task completed for pool-1-thread-7 result should be 7 task completed for pool-1-thread-6 result should be 6 task completed for pool-1-thread-5 result should be 5 task completed for pool -1-thread-4 result should be 4 task completed for pool-1-thread-3 result should be 3 task completed for ol-1-thread-2 result should be 2 task completed for pool-1-thread-1 result should be 1 future result equals 1
=================================================== =====
for executorservice, the result will be available after all tasks have been completed.
executor completeservice any result that can be obtained, make this return.
Prasad Apr 25 '19 at 16:09 2019-04-25 16:09
source share