[concurrency-interest] Bounded PriorityBlockingQueue

√iktor Ҡlang viktor.klang at gmail.com
Wed Jun 26 17:07:31 EDT 2013


Gustav,


On Wed, Jun 26, 2013 at 4:51 PM, Gustav Åkesson
<gustav.r.akesson at gmail.com>wrote:

> 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.
>

Thanks! Yeah, Scala is pretty easy on the eyes at times :)


> 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.
>

Exactly, which is why I am requiring java.util.Queue.


>
> 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().
>

Ouch! Nice catch, will fix.


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

You're most welcome, have fun :)

Cheers,
√


>
>
> 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
>>
>
>


-- 
*Viktor Klang*
*Director of Engineering*
Typesafe <http://www.typesafe.com/>

Twitter: @viktorklang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130626/5f4cd233/attachment-0001.html>


More information about the Concurrency-interest mailing list