Description
On the examples, could RangePublisher rely on volatile variable rather than on AtomicLong
?
From the examples, the RangePublisher
's Subscription extends an AtomicLong. It does so because its methods can be run from different threads. The following comment can be read:
We are using this
AtomicLong
to make sure that thisSubscription
doesn't run concurrently with itself, which would violate rule 1.3 among others (no concurrent notifications). The atomic transition from 0L to N > 0L will ensure this.
However, from rule 2.7
A Subscriber MUST ensure that all calls on its Subscription's request and cancel methods are performed serially.
Would this then not mean that, regardless of how the subscriber behaves, the subscription only needs to be concerned about publishing the index of that subscriber? In which case a volatile variable would suffice?
Provided I am right, using a less powerful form of synchronization is preferable by the principle of least power. More importantly, for people learning via these examples, usage of volatile
would emphasize that the Subscriber
must comply with rule 2.7.
What do you think?