|
| 1 | +package multithread; |
| 2 | + |
| 3 | +import java.util.concurrent.*; |
| 4 | + |
| 5 | +public class CompletionServiceDemo { |
| 6 | + /** |
| 7 | + * credit: https://stackoverflow.com/a/5580058/4117496 |
| 8 | + */ |
| 9 | + class CalcResult { |
| 10 | + long result; |
| 11 | + |
| 12 | + CalcResult(long l) { |
| 13 | + result = l; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + class CallableTask implements Callable<CalcResult> { |
| 18 | + String taskName; |
| 19 | + long input1; |
| 20 | + int input2; |
| 21 | + |
| 22 | + CallableTask(String name, long v1, int v2) { |
| 23 | + taskName = name; |
| 24 | + input1 = v1; |
| 25 | + input2 = v2; |
| 26 | + } |
| 27 | + |
| 28 | + public CalcResult call() { |
| 29 | + System.out.println("Task " + taskName + " started -----"); |
| 30 | + for (int i = 0; i < input2; i++) { |
| 31 | + try { |
| 32 | + Thread.sleep(200); |
| 33 | + } catch (InterruptedException e) { |
| 34 | + System.out.println("Task " + taskName + " interrupted !! "); |
| 35 | + e.printStackTrace(); |
| 36 | + } |
| 37 | + input1 += i; |
| 38 | + } |
| 39 | + System.out.println("Task " + taskName + " completed."); |
| 40 | + return new CalcResult(input1); |
| 41 | + } |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + public void test() { |
| 46 | + ExecutorService executorService = Executors.newFixedThreadPool(3); |
| 47 | + CompletionService<CalcResult> completionService = new ExecutorCompletionService<>(executorService); |
| 48 | + |
| 49 | + int submittedTasks = 5; |
| 50 | + for (int i = 0; i < submittedTasks; i++) { |
| 51 | + completionService.submit(new CallableTask( |
| 52 | + String.valueOf(i), |
| 53 | + (i * 10), |
| 54 | + ((i * 10) + 10) |
| 55 | + )); |
| 56 | + System.out.println("Task " + i + " submitted"); |
| 57 | + } |
| 58 | + for (int tasksHandled = 0; tasksHandled < submittedTasks; tasksHandled++) { |
| 59 | + try { |
| 60 | + System.out.println("trying to take from Completion service"); |
| 61 | + Future<CalcResult> result = completionService.take(); |
| 62 | + System.out.println("result for a task available in queue. Trying to get() now"); |
| 63 | + // above call blocks till atleast one task is completed and results availble for it |
| 64 | + // but we don't have to worry which one |
| 65 | + |
| 66 | + // process the result here by doing result.get() |
| 67 | + CalcResult l = result.get(); |
| 68 | + System.out.println("Task " + tasksHandled + " completed - results obtained : " + l.result); |
| 69 | + |
| 70 | + } catch (InterruptedException e) { |
| 71 | + // Something went wrong with a task submitted |
| 72 | + System.out.println("Error Interrupted exception"); |
| 73 | + e.printStackTrace(); |
| 74 | + } catch (ExecutionException e) { |
| 75 | + // Something went wrong with the result |
| 76 | + e.printStackTrace(); |
| 77 | + System.out.println("Error get() threw exception"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(String... args) { |
| 83 | + CompletionServiceDemo demo = new CompletionServiceDemo(); |
| 84 | + demo.test(); |
| 85 | + System.out.println("\n\nProgram finished.\n"); |
| 86 | + } |
| 87 | +} |
0 commit comments