[concurrency-interest] Separate implementations of CarrierSender and CarrierReceiver, and firing mechanism

Alexei Kaigorodov alexei.kaigorodov at gmail.com
Wed Mar 11 11:26:20 EDT 2020

If we make:
 - separate implementations of CarrierSender and CarrierReceiver, 
 - mechanism that watches their state, and when both instances are ready
(!CarrierSender.isEmpty() && !CarrierReceiver.isFull()) and not closed,
fires a user-defined method (say, run()), 

then the whole implementation of the Carrier becomes trivial:

class CarrierImpl<T> implements Carrier<T> {
    CarrierReceiver<T> receiver;
    CarrierSender <T> sender;

   public CarrierImpl() {
      // initialize sender and receiver

   protected void run() {
     if (receiver.isClosed) {
     T item= receiver.receive();

// a lot of delegating methods like
   void send(T item) throws ClosedException, CancellationException {

(I omitted some subtle nuances like how to provide serial execution of the
run() method and avoid violations of the message order).

This implementation is not the most performant, but can be used as a basis
of numerous other use cases:

1) allow user to override method run() and create simple message processor.
2) let CarrierSender implement Flow.Publisher and CarrierReceiver implement
Flow.Subscriber, and we have basic implementation of Flow.Processor
3) extend firing mechanism so that it can watch arbitrary set of Carriables,
and we have a basis for  processors with arbitrary number of inputs and
outputs, including pure producers and pure consumers.
4) expose implementations of CarrierReceiver and CarrierSender to user and
allow user to extend that classes. Very quickly implementations of
org.reactivestreams.{Publisher, Subscriber} and 
reactor.core.publisher.{Mono, Flux} appear, and JDK would become
interoperable with any existing and future synchronous and asynchronous
message-passing libraries.

The killer feature of this approach is the firing mechanism, which is easy
to implement.

Sent from: http://jsr166-concurrency.10961.n7.nabble.com/

More information about the Concurrency-interest mailing list