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(); } } 
  • phaser one, he has a counter, he does not need to understand or attach the thread, what did you mean by these words, what does it mean to be empty? - Russtam 8:50 pm
  • If you are given an exhaustive answer, mark it as correct (tick the selected answer). - andreycha

1 answer 1

Phaser is a kind of barrier, similar to CyclicBarrier and CountDownLatch , but has a more flexible setting.

The barrier in general is the following:

  • sets the number of threads that should be collected on the barrier
  • every stream arriving at the barrier is blocked
  • as soon as the number of flows specified above is collected on the barrier, the barrier “opens” and all flows continue their work

You can read more about the barriers (including about these three implementations) and see an illustration of their work in the article on Habré .

In the above code, the Phaser works like this:

  • initialized by the value of participants 1 + characters.size()
  • further streams in the number of characters.size() are registered in it and blocked
  • when you call the arriveAndDeregister() method, the last participant is registered, the barrier opens, all threads continue their work. At the same time, this method automatically reduces the number of registered participants for the next stage, but in this code it does not matter.

Answering specific questions:

By 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?

Yes.

Phaser understands that threads are its participants, due to the methods of arriveAndAwaitAdvance and arrive?

Yes.

Without calling them in this task it will be empty?

It's not entirely clear what you mean. It will be "empty" in the sense that the count of registered participants in it will be zero.


PS By the way, you have a problem with the isEveryoneReady field - access to it is not synchronized and you may receive several messages “The game has started!”.

  • Thank you, I have not come here for a long time. But I remembered everything at once) - PodKrepkimChaem
  • @PodKrepkimChaem is not for nothing! - andreycha