[concurrency-interest] Bounded PriorityBlockingQueue

Gustav Åkesson gustav.r.akesson at gmail.com
Wed Jun 26 16:51:42 EDT 2013


Sorry for the spam - still figuring out how to reply properly on e-mails.
;-)

------

Heinz,

Your mean like acquiring a permit when putting and releasing when taking? I
thought of such a solution but I didn't like introducing that extra
contention point of a Semaphore besides the queue's internal ReentrantLock
(and a lock-based collection is needed in such a case since Semaphore
doesn't provide mutual exclusion). Additionally, I wanted to get rid of
some of the PriorityBlockedQueue characteristics such as size() not being
constant in time. I now Dough hates size() in a concurrent collection, but
it could be useful to perform some sort of estimation.

Performance still matters (ideally the same as a ABQ), but needed
prioritization to enforce that important tasks always got executed before
any other.

_______________________________________

Viktor,

Nice implementation (gosh, Scala is pretty damn nice) - your general
solution isn't too different from my more specific. One concern I have is
the backing.size() which could potentially be expensive, especially in the
context of repeatedly trying to get the lock. On the other hand, if I'm not
mistaken, the collection's with size of linear cost usually belongs to the
concurrent ones, and I don't see any point in using a concurrent as backing
when using your implementation.

Also, the drainTo methods don't signal that the queue (potentially) has
been drained of element(s). That could lead to threads waiting for signal
to insert when the queue is in fact not full. Same thing with clear().

Got a couple of tips from it, though, which I will push to Github. Will
also push the basic tests. Thanks for input!


Best Regards,

Gustav Åkesson


On Wed, Jun 26, 2013 at 8:48 PM, Dr Heinz M. Kabutz <
heinz at javaspecialists.eu> wrote:

> Hi Gustav,
>
> Would it not be a lot easier to wrap a PriorityBoundedQueue with
> another class that simply uses a Semaphore to limit the total number
> of elements inside the queue?  Goetz's book has an implementation for
> a BoundedQueue.
>
> Heinz
>
> On 26/06/2013, Gustav Åkesson <gustav.r.akesson at gmail.com> wrote:
> > Hi,
> >
> > I had a use case for a bounded priority blocking queue in a large-scale
> > server but couldn't find any useful implementation. Below is my
> > implementation:
> >
> >
> https://github.com/gakesson/ConcurrencyUtils/blob/c5794c5b7c0ada763549cadfbbbd345713ace79a/BoundedPriorityBlockingQueue.java
> >
> > Feel free to have a look at it, test it and come up with feedback and
> > improvements. Also, if you have had the same requirements as I, feel free
> > to use it but don't forget to contribute if/when you find improvements.
> :-)
> >
> >
> > Best Regards,
> >
> > Gustav Åkesson
> > Cogitel AB
> > Sweden
> >
>
>
> --
> Dr Heinz M. Kabutz (PhD CompSci)
> Author of "The Java(tm) Specialists' Newsletter"
> Sun/Oracle Java Champion 2005-2013
> JavaOne Rockstar Speaker 2012
> http://www.javaspecialists.eu
> Tel: +30 69 75 595 262
> Skype: kabutz
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130626/f269e1c9/attachment.html>


More information about the Concurrency-interest mailing list