|
1 | 1 | package com.thealgorithms.sorts; |
2 | 2 |
|
3 | | -import java.util.concurrent.ExecutionException; |
4 | | -import java.util.concurrent.Future; |
| 3 | +import java.util.concurrent.CompletableFuture; |
5 | 4 | import java.util.concurrent.LinkedBlockingQueue; |
6 | 5 | import java.util.concurrent.ThreadPoolExecutor; |
7 | 6 | import java.util.concurrent.TimeUnit; |
@@ -83,18 +82,13 @@ private static void concurrentMergeSort(int[] array, int[] temp, int left, int r |
83 | 82 | int mid = left + (right - left) / 2; |
84 | 83 |
|
85 | 84 | // Submit the left half for concurrent execution |
86 | | - Future<?> leftTask = executor.submit(() -> concurrentMergeSort(array, temp, left, mid, executor, depth - 1)); |
| 85 | + CompletableFuture<Void> leftTask = CompletableFuture.runAsync(() -> concurrentMergeSort(array, temp, left, mid, executor, depth - 1), executor); |
87 | 86 |
|
88 | 87 | // Process the right half in the current thread to optimize resource usage |
89 | 88 | concurrentMergeSort(array, temp, mid + 1, right, executor, depth - 1); |
90 | 89 |
|
91 | | - try { |
92 | | - // Wait for the concurrently executed left half to complete |
93 | | - leftTask.get(); |
94 | | - } catch (InterruptedException | ExecutionException e) { |
95 | | - Thread.currentThread().interrupt(); |
96 | | - throw new IllegalStateException("Concurrent execution interrupted or failed", e); |
97 | | - } |
| 90 | + // Wait for the concurrently executed left half to complete |
| 91 | + leftTask.join(); |
98 | 92 |
|
99 | 93 | merge(array, temp, left, mid, right); |
100 | 94 | } |
|
0 commit comments