2

I have implemented MyListener which is accessed from two different threads.

class MyListener implements Listener {
    private final Phaser phaser = new Phaser(2);        

    @Override
    public void changed () {
        phaser.arrive();
    }

    public void await () {
        phaser.arriveAndAwaitAdvance();    
    } 
}

I use it like this in main thread

MyListener listener = new Listener();
someObject.setListener(listener);     

doSomething(); //it would result in Listener.changed() being invoked
listener.await();

doSomething(); //it would result in Listener.changed() being invoked
listener.await();

I have several concerns:

  1. Java docs states that calling arrive() and arriveAndAwaitAdvance() without calling register() first is not correct.
  2. changed() might be invoked several times for the same event. I expect that one of await() might return immideately because of previous event.

Any idea about this concerns?

Denim Datta
  • 3,740
  • 3
  • 27
  • 53
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101

1 Answers1

1

Regarding your first concern: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html#arrive()

says:

It is a usage error for an unregistered party to invoke this method.

It does not state that the registration has to take place by calling register(). Specifying the number of pre-registered parties in the constructor as you do is a valid usage.


About the second, you must not call arrive multiple times. It is not clear what your intention of calling changed() several times for one event shall imply, is the other thread allowed to proceed on the first invocation, the last one (how to know that it is the last one?) or something in between?

It seems that you want to split the arrival, i.e. giving the other process the right to proceed, from the waiting for this permission. This is quite easy: don’t use the compound arriveAndAwaitAdvance():

int currentPhase = phaser.arrive(); // allow the other thread to proceed
…
phaser.awaitAdvance(currentPhase); // wait for the other thread’s arrival
Holger
  • 285,553
  • 42
  • 434
  • 765
  • What this line actually mean or try to mean? "It is a usage error for an unregistered party to invoke this method." – neonant Sep 07 '16 at 11:08
  • exactly what it says on the tin – Holger Sep 07 '16 at 11:18
  • Sorry, I didn't get you. What does it mean by "unregistered party"? – neonant Sep 07 '16 at 11:42
  • I hope, you noticed that this is a cite from [the documentation](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html). Did you read (and understand) it? – Holger Sep 07 '16 at 11:44
  • Surely, I did. Why would I not!? I just didn't understand the meaning/context of "unregistered party" used in the doc(or the line you cited). Did you understand it? If yes, then please let me know your words. – neonant Sep 07 '16 at 11:50
  • What word do you have difficulties with? “party”? That term is used a hundred times in the documentation. “unregistered”? It’s to opposite of “registered” and the fact that there is a number of registered parties is fundamental to that class, that’s why you can [register a number of parties in the constructor](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html#Phaser(int)) or [register](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html#register()) them on by one, etc. I have no idea what you can’t understand in that context. – Holger Sep 07 '16 at 11:58