[concurrency-interest] Is LinkedBlockingQueue.isEmpty() thread safe?

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Sun Apr 22 15:36:01 EDT 2007


On 22/04/07, Brian Goetz <brian at quiotix.com> wrote:

> In that light, I offer a credible example: a single-producer,
> multiple-consumer situation.  Since the single producer is the only one
> calling put(), having the producer call isEmpty() and receive "true"
> would allow it to conclude "everything that's been put on the queue has
> been processed, so it is safe to shut down now."

You really make me wonder: How would your producer look like?

Thread t = new Thread {
  public void run() {
    while (thereAreItemsToProduce()) {
      q.put(item(n));
    }
    while (!q.isEmpty)
      ;
  }
};

Or you would insert a deschedule into your busy loop as well?

    while (!q.isEmpty)
      Thread.yield();

I only hope you would not check the emptiness inside an `if' statement.

Wouldn't you just simply leave out the busy loop? Wouldn't it be simpler?

Thread t = new Thread {
  public void run() {
    while (thereAreItemsToProduce()) {
      q.put(item(n));
    }
  }
};

I am really curious.

After all, concrete examples are I am begging all along.

Best Regards,
Szabolcs


More information about the Concurrency-interest mailing list