There's more...

In this example we used the addToPendingCount() method to increment the counter of pending tasks, but we have other methods we can use to change the value of this counter:

  • setPendingCount(): This method establishes the value of the counter of pending tasks.
  • compareAndSetPendingCount(): This method receives two parameters. The first one is the expected value and the second one is the new value. If the value of the counter of pending tasks is equal to the expected value, establish its value to the new one.
  • decrementPendingCountUnlessZero(): This method decrements the value of the counter of pending tasks unless it's equal to zero.

The CountedCompleter class also includes other methods to manage the completion of the tasks. ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.