Skip to content

Commit

Permalink
Start threads; then wait for results
Browse files Browse the repository at this point in the history
Signed-off-by: acarbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Oct 12, 2023
1 parent 3cee673 commit 8dcfec0
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,26 @@ public static void testClientSetGet(
concurrentNum, clientCount, tasks.size());
}
long before = System.nanoTime();
tasks.stream()
.map(CompletableFuture::runAsync)
.forEach(
f -> {
try {
f.get();
} catch (Exception e) {
e.printStackTrace();
}
});

// create threads and add them to the asyncpool.
// This will start execution of all the concurrent tasks.
List<CompletableFuture> asyncTasks =
tasks.stream().map(CompletableFuture::runAsync).collect(Collectors.toList());
try {
// wait 1 second before waiting for threads to complete
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
// wait for all futures to complete
asyncTasks.forEach(
future -> {
try {
future.get();
} catch (Exception e) {
e.printStackTrace();
}
});
long after = System.nanoTime();

// print results per action
Expand Down

0 comments on commit 8dcfec0

Please sign in to comment.