j.u.c.Flow Javadoc example of OneShotPublisher incorrect: onSubscribe not called if subscribed == true

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

j.u.c.Flow Javadoc example of OneShotPublisher incorrect: onSubscribe not called if subscribed == true

JSR166 Concurrency mailing list
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

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest