Queue with interrupt

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Queue with interrupt

Jochen Theodorou
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
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

David Holmes-6
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:[hidden email]] On Behalf Of Jochen Theodorou
> Sent: Wednesday, May 17, 2017 4:15 PM
> To: [hidden email]
> 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
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Viktor Klang
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 <[hidden email]> 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:[hidden email]] On Behalf Of Jochen Theodorou
> Sent: Wednesday, May 17, 2017 4:15 PM
> To: [hidden email]
> 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
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



--
Cheers,

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Jochen Theodorou
In reply to this post by David Holmes-6


On 17.05.2017 08:31, David Holmes 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.
>

I think maybe it is better to show some code...
See
https://github.com/canoo/dolphin-platform/blob/master/platform/dolphin-platform-server/src/main/java/com/canoo/dolphin/server/context/DolphinContextTaskQueue.java

addTasks and interrupt are called from a different thread than
executeTasks. executeTasks is responsible for one iteration of task
executions, not a continuously blocking worker.

I do not want to interrupt task execution of course, only the waiting.

So your suggestion is to add a sentinel task... I guess that works, as
long as I can add the task before already existing tasks, that may be
still in the queue, so that it can interrupt between task executions (
not during of course)

bye Jochen
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

David Holmes-6
> -----Original Message-----
> From: Jochen Theodorou [mailto:[hidden email]]
> Sent: Wednesday, May 17, 2017 7:26 PM
> To: [hidden email]; [hidden email]
> Subject: Re: [concurrency-interest] Queue with interrupt
>
>
>
> On 17.05.2017 08:31, David Holmes 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.
> >
>
> I think maybe it is better to show some code...
> See
> https://github.com/canoo/dolphin-platform/blob/master/platform/dolphin-platform-
> server/src/main/java/com/canoo/dolphin/server/context/DolphinContextTaskQueue.java
>
> addTasks and interrupt are called from a different thread than executeTasks. executeTasks is responsible for one iteration of task
> executions, not a continuously blocking worker.
>
> I do not want to interrupt task execution of course, only the waiting.
>
> So your suggestion is to add a sentinel task... I guess that works, as long as I can add the task before already existing tasks, that may be
> still in the queue, so that it can interrupt between task executions ( not during of course)

If there are tasks in the queue then the worker is not waiting for new tasks to do - so you've changed your requirements. You would need a deque to allow sentinel tasks to be forced to the head of the queue.

David

 
> bye Jochen

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Jochen Theodorou
In reply to this post by Viktor Klang
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 <[hidden email]
> <mailto:[hidden email]>> 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:[hidden email]
>     <mailto:[hidden email]>] On Behalf Of
>     Jochen Theodorou
>     > Sent: Wednesday, May 17, 2017 4:15 PM
>     > To: [hidden email]
>     <mailto:[hidden email]>
>     > 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
>     > [hidden email]
>     <mailto:[hidden email]>
>     > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>
>     _______________________________________________
>     Concurrency-interest mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>
>
>
>
> --
> Cheers,
> √
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Jochen Theodorou
In reply to this post by David Holmes-6


On 17.05.2017 11:29, David Holmes wrote:
[...]
> If there are tasks in the queue then the worker is not waiting for new tasks to do - so you've changed your requirements. You would need a deque to allow sentinel tasks to be forced to the head of the queue.


ah... now I see what I have overlooked.. I should have been using
Dequeue/LinkedBlockingDeque instead.

yes, that explains also why I had the feeling I am doing something stupid ;)

Thanks

bye Jochen
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Aaron Grunthal
In reply to this post by Jochen Theodorou
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 <[hidden email]
>> <mailto:[hidden email]>> 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:[hidden email]
>>     <mailto:[hidden email]>] On Behalf Of
>>     Jochen Theodorou
>>     > Sent: Wednesday, May 17, 2017 4:15 PM
>>     > To: [hidden email]
>>     <mailto:[hidden email]>
>>     > 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
>>     > [hidden email]
>>     <mailto:[hidden email]>
>>     > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>>
>>     _______________________________________________
>>     Concurrency-interest mailing list
>>     [hidden email]
>>     <mailto:[hidden email]>
>>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>     <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>>
>>
>>
>>
>> --
>> Cheers,
>> √
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Chris Hegarty
In reply to this post by David Holmes-6

> On 17 May 2017, at 10:29, David Holmes <[hidden email]> wrote:
>
> ...
> If there are tasks in the queue then the worker is not waiting for new tasks to do - so you've changed your requirements. You would need a deque to allow sentinel tasks to be forced to the head of the queue.

I’ve always wished that we could somehow squeeze a sentinel into the
implementation in order to provide a “CloseableBlockingQueue” with a
blocking takeOrNull(), or similarly named, method. Rather than folk having
to roll their own.

-Chris.

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Viktor Klang
OTOH, I'd rather avoid Blocking* whenever possible.

On Thu, May 18, 2017 at 1:43 PM, Chris Hegarty <[hidden email]> wrote:

> On 17 May 2017, at 10:29, David Holmes <[hidden email]> wrote:
>
> ...
> If there are tasks in the queue then the worker is not waiting for new tasks to do - so you've changed your requirements. You would need a deque to allow sentinel tasks to be forced to the head of the queue.

I’ve always wished that we could somehow squeeze a sentinel into the
implementation in order to provide a “CloseableBlockingQueue” with a
blocking takeOrNull(), or similarly named, method. Rather than folk having
to roll their own.

-Chris.

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



--
Cheers,

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Queue with interrupt

Gregg Wonderly-3
In reply to this post by Chris Hegarty
Many times I use a subclass of one of the queue implementations that I pass a signaling Object to.  That subclass overrides all methods that mutate the state of the queue, and the overriding methods have

synchronized( lockObj ) {
super.XXXXX();
lockObj.notifyAll();
}

in each of them so that they tell the lock waiters that something has changed.  The lack of a value to account for false signaling can be remedied by using a functional interface to signally instead of the literal Object signaling.

Then, I will also have a timer that fires to signal periodically to the same signal object, perhaps using a secondary signal type.

public interface ActionSignalHandler<E> {
void QueueModified( SignallingQueue queue );
void TimerFired( Timer which );
void WorkCompleted( E work );
}

or some such.   This style of API provides the functionality I need generally when I have one or more queues interactive with one or more timers and have an environment such as Swing/JavaFX/AWT which needs a specific thread type against background tasks managing data values and the occasional ‘ticking’ task needed for progress indication or other change of state recognition or remote service integration.

Gregg

On May 18, 2017, at 6:56 AM, Viktor Klang <[hidden email]> wrote:

OTOH, I'd rather avoid Blocking* whenever possible.

On Thu, May 18, 2017 at 1:43 PM, Chris Hegarty <[hidden email]> wrote:

> On 17 May 2017, at 10:29, David Holmes <[hidden email]> wrote:
>
> ...
> If there are tasks in the queue then the worker is not waiting for new tasks to do - so you've changed your requirements. You would need a deque to allow sentinel tasks to be forced to the head of the queue.

I’ve always wished that we could somehow squeeze a sentinel into the
implementation in order to provide a “CloseableBlockingQueue” with a
blocking takeOrNull(), or similarly named, method. Rather than folk having
to roll their own.

-Chris.

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



--
Cheers,
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Loading...