Производитель хочет дождаться завершения всех задач, прежде чем продолжить.
Сторона производителя
// Add producer as a party Phaser phaser = new Phaser(1); for (int i=0; i<10000; ++i) { // Add each task as a party phaser.register(); queue.put(new Task()); } // Producer arrived and wait for completion of all tasks phaser.arriveAndAwaitAdvance(); // At the end, there is only 1 party left which is the producer itself
Сторона потребителя
while (true) { Task task = queue.take(); processTask(task); // Task completed and remove itself as a party phaser.arriveAndDeregister(); }
Оригинал: “https://dev.to/franzwong/wait-for-completion-of-all-tasks-with-java-phaser-1g8m”