|
10 | 10 |
|
11 | 11 | import static junit.framework.TestCase.assertTrue; |
12 | 12 |
|
13 | | -public class WaitingForThreadsToFinishTest { |
| 13 | +public class WaitingForThreadsToFinishManualTest { |
14 | 14 |
|
15 | | - private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishTest.class); |
| 15 | + private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishManualTest.class); |
16 | 16 | private final static ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10); |
17 | 17 |
|
18 | 18 | public void awaitTerminationAfterShutdown(ExecutorService threadPool) { |
@@ -142,66 +142,4 @@ public void givenMultipleThreads_whenUsingCompletionService_thenMainThreadShould |
142 | 142 | awaitTerminationAfterShutdown(WORKER_THREAD_POOL); |
143 | 143 | } |
144 | 144 | } |
145 | | - |
146 | | - @Test |
147 | | - public void givenMultipleThreads_whenUsingCompletableFutures_thenMainThreadShouldWaitForAllToFinish() { |
148 | | - |
149 | | - CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> { |
150 | | - |
151 | | - try { |
152 | | - Thread.sleep(1000); |
153 | | - } catch (InterruptedException e) { |
154 | | - e.printStackTrace(); |
155 | | - } |
156 | | - |
157 | | - return "Hello"; |
158 | | - }); |
159 | | - |
160 | | - CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> { |
161 | | - |
162 | | - try { |
163 | | - Thread.sleep(5000); |
164 | | - } catch (InterruptedException e) { |
165 | | - e.printStackTrace(); |
166 | | - } |
167 | | - |
168 | | - return "Beautiful"; |
169 | | - }); |
170 | | - |
171 | | - CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> { |
172 | | - |
173 | | - try { |
174 | | - Thread.sleep(3000); |
175 | | - } catch (InterruptedException e) { |
176 | | - e.printStackTrace(); |
177 | | - } |
178 | | - |
179 | | - return "World"; |
180 | | - }); |
181 | | - |
182 | | - long startProcessingTime = System.currentTimeMillis(); |
183 | | - CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3); |
184 | | - combinedFuture.join(); |
185 | | - |
186 | | - long totalProcessingTime = System.currentTimeMillis() - startProcessingTime; |
187 | | - assertTrue(totalProcessingTime >= 5000 && totalProcessingTime < 6000); |
188 | | - |
189 | | - LOG.debug("Responses from all threads are available after " + totalProcessingTime + " milliseconds"); |
190 | | - |
191 | | - try { |
192 | | - String thread1Response = future1.get(); |
193 | | - assertTrue(thread1Response.equals("Hello")); |
194 | | - |
195 | | - String thread2Response = future2.get(); |
196 | | - assertTrue(thread2Response.equals("Beautiful")); |
197 | | - |
198 | | - String thread3Response = future3.get(); |
199 | | - assertTrue(thread3Response.equals("World")); |
200 | | - |
201 | | - } catch (InterruptedException | ExecutionException e) { |
202 | | - e.printStackTrace(); |
203 | | - } |
204 | | - |
205 | | - awaitTerminationAfterShutdown(WORKER_THREAD_POOL); |
206 | | - } |
207 | 145 | } |
0 commit comments