[concurrency-interest] Class striped/ordered Thread Pool

Pavel Rappo pavel.rappo at gmail.com
Fri May 11 09:42:56 EDT 2012


Once I had the same problem and solved it exactly like Dr. Kabutz explained. That's pretty straightforward.
In my case there were m users connected to CCTV server with n worker threads (m >> n). Each of the users issued standard commands to cameras like:
	Left (10 deg)->Up (30 deg)->Zoom (1.5x).
Obviously I had to preserve the order of these commands.

On 11 May 2012, at 12:23, Dr Heinz M. Kabutz wrote:

> Hi Glenn,
> 
> I think your idea can be implemented fairly easily.  I wrote something similar yesterday, where I had queues of different priority and also stored them in a map of queues.
> 
> First off, you need a special type of BlockingQueue that takes your StripedRunnable class and pops it into a queue for each specific stripe object.  I don't think your stripe object needs to have a hash code function.  I would use a type of identity hash map with weak references for keys.
> 
> Inside each queue you would have a gate that would start open.  The offer() method would simply add elements to the queue without affecting the gate.  Whenever an element is removed by the thread pool, the gate would be closed.  This means that if another thread wanted to remove an element, it would skip over that queue since the gate is closed.  Once the job has been completed, the job would again open the gate.  This is easily done by wrapping the job with another Runnable that controls the gate.
> 
> I don't think you need to change anything in your thread pool to get this to work.  All you need is the right type of queue.
> 
> If you like, I can try and write it for you tomorrow on my travels back to Greece from Germany.  No charge of course.
> Regards
> 
> Heinz
> -- 
> Dr Heinz M. Kabutz (PhD CompSci)
> Author of "The Java(tm) Specialists' Newsletter"
> Sun Java Champion
> IEEE Certified Software Development Professional
> 
> http://www.javaspecialists.eu
> 
> Tel: +30 69 75 595 262
> Skype: kabutz 
> 
> 
> 
> On 5/11/12 7:14 AM, Glenn McGregor wrote:
>> On 5/10/2012 10:07 PM, David Holmes wrote:
>>> Not sure if this makes sense unless you use a SingleThreadExecutor per stripeClass. Otherwise you have races in the submission of StripedRunners of the same stripeClass, and even if you order the queue so that they start execution in the right order, you can't guarantee what order they will complete in. Unless it is all done by a single thread.
>>>  
>>> David Holmes
>> 
>> I can guarantee that items of the same class are generated by one thread.
>> 
>> To expand on my particular use case...
>> 
>> I have a small number of threads using NIO to read multiple TCP or SCTP data streams.
>> I need to process TLS in order for a given socket.
>> But a given TLS stream is only read by one thread.
>> 
>> So what I want to hand off my TLS work to a thread pool, but need to keep particular streams in sequence.
>> 
>> Thanks
>> 
>> 
>> Glenn McGregor
>> 
>> 
>>> -----Original Message-----
>>> From: concurrency-interest-bounces at cs.oswego.edu [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Glenn McGregor
>>> Sent: Friday, 11 May 2012 2:54 PM
>>> To: concurrency-interest at cs.oswego.edu
>>> Subject: [concurrency-interest] Class striped/ordered Thread Pool
>>> 
>>> Hello all, 
>>> 
>>> I'm looking for a thread pool with the following characteristics... (in pseudo-code) 
>>> 
>>> interface StripedRunner { 
>>>     Object getStripeClass(); 
>>>     void run(); 
>>> } 
>>> 
>>> magicPool.execute( stripedRunner ); 
>>> 
>>> The idea being that any StripedRunners with the same stripeClass would get executed in the order they were queued, 
>>> but StripedRunners with different stripedClasses could execute independently. 
>>> 
>>> Is there a way to configure the standard concurrent pools to work this way (doesn't seem to be), or a way to extends 
>>> the current code? 
>>> 
>>> Or perhaps some third party library implementation? 
>>> 
>>> Thanks! 
>>> 
>>> Glenn McGregor 
>>> 
>> 
>> 
>> 
>> _______________________________________________
>> Concurrency-interest mailing list
>> 
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>> 
>>   
>> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest




More information about the Concurrency-interest mailing list