[concurrency-interest] j.u.c.Flow Javadoc example of OneShotPublisher incorrect: onSubscribe not called if subscribed == true

Dávid Karnok akarnokd at gmail.com
Wed Dec 6 10:03:19 EST 2017


Hi. The example OneShotPublisher violates the specification for the second
or later Subscribers because onSubscribe is not called before invoking the
onError(new IllegalStateException()).

http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/Flow.java?revision=1.32&view=markup

 *   public synchronized void subscribe(Subscriber<? super Boolean>
subscriber) {
 *     if (subscribed)
 *       subscriber.onError(new IllegalStateException()); // only one allowed
 *     else {
 *       subscribed = true;
 *       subscriber.onSubscribe(new OneShotSubscription(subscriber, executor));
 *     }
 *   }


Could be:

enum EmptySubscription implements Flow.Subscription {
   INSTANCE;
   @Override public void request(long n) { }
   @Override public void cancel() { }
}

public synchronized void subscribe(Flow.Subscriber<? super Boolean>
subscriber) {
  if (subscribed) {
    subscriber.onSubscribe(EmptySubscription.INSTANCE);
    subscriber.onError(new IllegalStateException()); // only one allowed
  } else {
    subscriber = true;
    subscriber.onSubscribe(new OneShotSubscription(subscriber, executor));
  }
}


-- 
Best regards,
David Karnok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20171206/e9db7e77/attachment.html>


More information about the Concurrency-interest mailing list