Can I use HawtDispatch for a single-threaded executor?

49 views
Skip to first unread message

Jon Brisbin

unread,
Oct 2, 2011, 9:46:59 AM10/2/11
to hawtdi...@googlegroups.com
I'm trying to create a Java ExecutorService implementation that, when submitted a Runnable that implements some special functionality I'm calling ThreadAffinity, will schedule that task on the thread to which it was previously assigned. This way all my event handlers are executed by the same thread. The source of these events comes from a possibly different, random Thread, so I need to maintain single-threaded behaviour by enqueueing the work to be done on that Thread's work queue. The reason for maintaining single-threaded behaviour is because these actions form individual components of a state machine, and are order-dependent. This is all happening in an essentially stateless, asynchronous environment.

My question is: can I use HawtDispatch to achieve this functionality without the contention side effects I'm seeing in the current way I'm doing it (which is, admittedly, quite a barbaric way to go about doing it :)?

Currently, I have a subclass of Thread pulling tasks off a work queue and executing them. I'm simply submitting Runnables to the correct worker by pulling this thread assignment information from a special interface (the ThreadAffinity trait, which I mentioned earlier). I'm wondering if HawtDispatch couldn't help me achieve this better? I've not used HawtDispatch directly before, just within the confines of Akka Actors, so I thought I better ask the experts before trudging off on my own.


Thanks!

Jon Brisbin
http//jbrisbin.com

Hiram Chirino

unread,
Oct 3, 2011, 8:39:02 AM10/3/11
to hawtdi...@googlegroups.com
If you want you want in-order execution, just submit all your tasks to
a Serial Dispatch Queue [1].

[1] http://hawtdispatch.fusesource.org/#The_DispatchQueue

Regards,
Hiram

FuseSource
Web: http://fusesource.com/

Jon Brisbin

unread,
Oct 3, 2011, 9:08:59 AM10/3/11
to hawtdi...@googlegroups.com
Are serial dispatch queues single-threaded? I need to ensure that a series of events that are all related are guaranteed to be run on the same thread. I can't submit part of this computation to one thread and have the next segment run on a different thread, even if they are run in the correct order. I want zero context-switching...


Thanks!

Jon Brisbin
http//jbrisbin.com


Hiram Chirino

unread,
Oct 3, 2011, 3:30:51 PM10/3/11
to hawtdi...@googlegroups.com
Hi Jon,

Serial dispatch queues execute their tasks on only one thread at a
time, but my get moved from thread to thread depending on availability
of concurrent executors.

Serial queues will generally only switch between threads once they
drain all their queued tasks. HawtDispatch does have some deprecated
API's to dispatch queues which really are pinned to a single Thread,
but I've failed to see cases where using those APIs produce much
better results than serial dispatch queues. More often than not,
using those APIs, leads to thread starvation situations.

Regards,
Hiram

FuseSource
Web: http://fusesource.com/

Reply all
Reply to author
Forward
0 new messages