java 8 parallel stream, blockingcode possible?

Viewed 5379

My situation is quite simple.

I have a list I want to perform logic on each item asynchronically.

when all the threads are done, i want to call a close connection.

like so:

bucketsList.parallelStream().forEach(t -> {
//some logic
});
 try {
 RestApi.getInstance().closeClientConnection();
 } catch (IOException e) {
     e.printStackTrace();
 }

is there a way to make the closeConnection part wait for the parallel Stream to finish going through all of its objects?

EDIT: I cannot use CountDownLatch as i dont know how many items i will have in bucketsList

1 Answers
Related