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;
}
}
================================================== ====================
Executor 完了サービスのログの違い:現在のスレッドは 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-thread-9 結果は 9 になるはずです 結果は 9 になります プール 1-スレッド 8 のタスクが完了しました 結果は 8 になります プール 1-スレッド 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 である必要があります。 1-thread-9 結果は 9 である必要があります。タスクはプール 1-スレッド 8 で完了しました。結果は 8 である必要があります。タスクはプール 1-スレッド 7 で完了しました。結果は 7 である必要があります。 thread-6 結果は 6 である必要があります タスクはプール 1 に対して完了しました-thread-5 結果5 である必要があります プール 1-スレッド 4 で完了したタスク 結果は 4 である必要があります プール 1-スレッド 3 で完了したタスク 結果は 3 である必要があります プール 1-スレッド 2 で完了したタスク 結果は次のとおりです2 pool-1-thread-1 のタスクが完了しました。結果は 1 である必要があります。将来の結果は 1 です。
================================================== =====
executorservice の場合、結果はすべてのタスクが完了した後にのみ利用可能になります。
エグゼキュータの完了サービスは、利用可能な結果を返します。