How, in this assignment, does the Phaser
understand that the threads are its participants?
arriveAndAwaitAdvance()
calling the method arriveAndAwaitAdvance()
, does it attach the thread in which it was called to the passer in the state it arrived at the barrier?
Phaser
understands that threads are its participants, due to the methods of arriveAndAwaitAdvance
and arrive
?
Without calling them in this task it will be empty?
package com.javarush.test.level28.lesson10.home01; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Phaser; public class Solution { public static void main(String[] args) throws InterruptedException { List<Character> characters = new ArrayList<>(); characters.add(new Plant()); characters.add(new Plant()); characters.add(new Zombie()); characters.add(new Zombie()); characters.add(new Zombie()); start(characters); } private static boolean isEveryoneReady = false; private static void start(List<Character> characters) throws InterruptedException { final Phaser phaser = new Phaser(1 + characters.size()); for (final Character character : characters) { final String member = character.toString(); System.out.println(member + " присоединился к игре"); new Thread() { @Override public void run() { System.out.println(member + " готовится играть"); phaser.arriveAndAwaitAdvance(); if (!isEveryoneReady) { isEveryoneReady = true; System.out.println("Игра началась!"); } character.run(); } }.start(); } phaser.arriveAndDeregister(); } }