Max Ng
Max Ng

Reputation: 484

Multi producer single consumer queue without dedicated consumer thread

I'm writing an async application which submits elements to a work queue for processing. The requirements are:

Here is an example of what I'm trying to do:

final Queue<T> queue = new ConcurrentLinkedQueue<>();
final AtomicBoolean processing = new AtomicBoolean();

void offer(T t) {
    queue.offer(t);

    for (;;) {
        if (processing.compareAndSet(false, true)) {
            try {
                for (;;) {
                    T t = queue.poll();
                    if (t == null) {
                        break;
                    }
                    // process t
                }
            } finally {
                processing.set(false);
            }

            // see if another thread submitted an element but didn't process it
            // if not, we can break
            if (queue.isEmpty()) {
                break;
            }
        } else {
            // losers should exit
            break;
        }
    }
}

I think my code works, but this problem sounds fairly standard so I would like to know if there is any textbook solution or at least a common name. So far I only find algorithms that require a worker thread to be listening on the queue.

Upvotes: 1

Views: 1005

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65936

Your code is correct, but problem is not seen as standard: usually, having background thread is not so expensive, as:

  1. Having no finite time garantee for producers. It is possible for some producer to work for a long time in consumer mode.

  2. Complicate producer's code.

In any case, your question's title (MPSC without dedicated consumer thread) describes the problem well. For resolve it you can combine trylock/unlock approach with any MPSC implementation without notification mechanism. The only additional requirement to MPSC is concurrent queue.isEmpty() method, but usually it is not a problem.

Upvotes: 1

Related Questions