[concurrency-interest] Is there a ConcurrentBlockingQueue ??

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Tue Mar 27 18:15:49 EDT 2007


Hi Hanson,

I have thought about it a bit more and I came to a very surprising
conclusion. (It is about the previous version not about the last
update.) The solution you provide is basically an optimised busy
loop. If we have a look at the very core of it, it looks like this
(when optimisation and interrupt handling is removed):

    public boolean offer(E e) {
        return q.offer(e);
    }
    public E take() throws InterruptedException
    {
	E e;
	do {
	    e = q.poll();
	} while (e == null);
	return e;
    }

The pack/unpack stuff is just some optimalisation sugar on it.

It functions exactly like your optimised one and performs the same or
slightly better for your N producer 1 consumer load test on a dual
core processor. BTW I think a better load test could be the reverse,
i.e. N consumer 1 producer, since the interesting issue is in the long
term scheduling of the consumer processes which is not exercised by
the N producer 1 consumer test.

In fact, your solution is the inverse of the conventional monitor-like
solution which would look like this:

    public synchronized boolean offer(E e) {
        boolean b = q.offer(e);
	notify();
	return b;
    }
    public synchronized E take() throws InterruptedException
    {
	while (q.isEmpty()) {
	    wait();
	}
	E e = q.poll();
	return e;
    }

I say inverse because in the monitor-like solution the effective
action follows the check. The check to true is the precondition of the
action. The check is performed in an optimised (see wait()) busy loop
too. In the monitor one needs the lock to keep the check and the
corresponding action together as an atomic action.---In your solution,
on the other hand, the lock is not necessary since the check follows
the action. At the time the check comes, the result is already private
to the thread so mutual exclusion is not needed.

Best Regards,
Szabolcs



On 27/03/07, Hanson Char <hanson.char at gmail.com> wrote:
> Here you go:
>
> http://svn.sourceforge.net/viewvc/beanlib/trunk/beanlib/src/net/sf/beanlib/util/concurrent/ConcurrentLinkedBlockingQueue.java
>
> Let me know if there is any reason I need to be blamed :)
>
> Hanson Char
>
> On 3/26/07, Hanson Char <hanson.char at gmail.com> wrote:
> > Thanks, Szabolcs.
> >
> > I will update the code in sourceforge shortly.  Stay tuned.
> >
> > Hanson Char
>


More information about the Concurrency-interest mailing list