[concurrency-interest] Queue with interrupt

Aaron Grunthal aaron.grunthal at infinite-source.de
Wed May 17 06:33:00 EDT 2017


You could simply drain all available tasks from the queue into a local list, then look for the wakeup before processing the rest.

On 17.05.2017 11:33, Jochen Theodorou wrote:
> But the Wakeup needs to be ordered in before any Job. Plus I have to retain the insertion order of the Jobs. I do not see PriorityBlockingQueue here, since I cannot tell if the sorting will retain
> insertion order... unless maybe if the elements get now wrapped in something with a count, then I sort according to this count ascending, and Wakeup would have the lowest possible count.... there is
> really no more simple solution?
> 
> bye Jochen
> 
> On 17.05.2017 08:40, Viktor Klang wrote:
>> If I interpret the email correctly, if the queue elements are of type
>> `Job | Wakeup`, then the second thread could offer a Wakeup if it wants
>> to interrupt the worker without having a Job.
>>
>> On Wed, May 17, 2017 at 8:31 AM, David Holmes <davidcholmes at aapt.net.au
>> <mailto:davidcholmes at aapt.net.au>> wrote:
>>
>>     Hi Jochen,
>>
>>     How are you "waiting for tasks"? As you note a Queue has no notion
>>     of blocking on empty. Using a BlockingQueue you can either interrupt
>>     the worker thread or send a sentinel task that simply indicates "do
>>     the other processing now". Of course I can't tell you know whether
>>     the worker is waiting or working - you can't arbitrarily "interrupt"
>>     the execution of a task.
>>
>>     David
>>
>>     > -----Original Message-----
>>     > From: Concurrency-interest
>>     [mailto:concurrency-interest-bounces at cs.oswego.edu
>>     <mailto:concurrency-interest-bounces at cs.oswego.edu>] On Behalf Of
>>     Jochen Theodorou
>>     > Sent: Wednesday, May 17, 2017 4:15 PM
>>     > To: concurrency-interest at cs.oswego.edu
>>     <mailto:concurrency-interest at cs.oswego.edu>
>>     > Subject: [concurrency-interest] Queue with interrupt
>>     >
>>     > Hi all,
>>     >
>>     > yesterday I was looking for a certain behaviour, but was not
>>     finding one and I am wondering if I did overlook something.
>>     >
>>     > Situation:
>>     >
>>     > I have 1 Thread - lets call it worker, that needs to execute a
>>     series of tasks. If there are no tasks this thread has to wait for a
>>     maximum
>>     > of 5 seconds. After this time I have to send something like an
>>     update from this thread (has to be the same thread) to later continue on
>>     > waiting for tasks or handle the new ones. At this point I will not
>>     car if processing the tasks takes longer than 5 seconds. And now the
>>     > specialty: A second thread must be able to send an interrupt to
>>     the worker thread, which than has to stop waiting for new jobs and
>>     > send the update right away, to then get back to waiting for jobs.
>>     I cannot send the update from the interrupting thread.
>>     >
>>     > Now I was expecting to find something in a Queue implementation,
>>     but while I did find a way to wait for new elements, I did not find a
>>     > wait to interrupt that waiting. We solved this by using a
>>     BlockingQueue, plus some signaling, but it really does not feel
>>     right to me.
>>     >
>>     > Any idea how to do this better?
>>     >
>>     > bye Jochen
>>     > _______________________________________________
>>     > Concurrency-interest mailing list
>>     > Concurrency-interest at cs.oswego.edu
>>     <mailto:Concurrency-interest at cs.oswego.edu>
>>     > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>>
>>     _______________________________________________
>>     Concurrency-interest mailing list
>>     Concurrency-interest at cs.oswego.edu
>>     <mailto:Concurrency-interest at cs.oswego.edu>
>>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>>
>>
>>
>>
>> -- 
>> Cheers,
>>> _______________________________________________
> 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