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

√iktor Ҡlang viktor.klang at gmail.com
Sun Feb 17 17:21:09 EST 2013


Here you have it:
https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/util/SerializedSuspendableExecutionContext.scala

If you're only on plain Java, search-and-replace ExecutionContext with
j.u.c.Executor

(It is also susspendable and resumable)

Cheers,
√


On Sun, Feb 17, 2013 at 10:35 PM, Jeff Hain <jeffhain at rocketmail.com> wrote:

> Hi.
>
>
>
>
> 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.
>
>
>
> -Jeff
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>


-- 
*Viktor Klang*
*Director of Engineering*
*
*
Typesafe <http://www.typesafe.com/> - The software stack for applications
that scale
Twitter: @viktorklang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130217/2d36bfa2/attachment.html>


More information about the Concurrency-interest mailing list