[concurrency-interest] Help on design a thread pool, some tasks need to executed in order

Jeff Hain jeffhain at rocketmail.com
Sun Feb 17 16:35:59 EST 2013


Feng Shen wrote:
>Would appreciate for any thought about how to design such a thread pool?

It seems to me you don't need a particular thread pool to have some of your
tasks ordered, if you can keep a reference to last submitted runnable for each
ordered sequence of runnables (for example using a map in IO thread).

I'm thinking about using a specific "LinkingRunnable", containing
an "impl" Runnable and an AtomicReference to a "next" Runnable.

When LinkingRunnable.run() is called, it first runs impl.run(), and then
does CAS(null,this) (using "this" as a tombstone): if CAS fails, that means
someone added a runnable to run next, and it runs it.

When subsequent task arrives, you try to "enqueue" it doing CAS(null,next):
If CAS succeeds, that means it'll be executed just after previous task
(and in same thread), and if it fails that means that the previous task
already completed, so you can just submit your next task to the pool.

You can also test "get() == null" before doing CASes, which should
make them rare, and reduce the usual overhead to a volatile read.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130217/70c29bdf/attachment.html>

More information about the Concurrency-interest mailing list