[concurrency-interest] Task Queue ordering

Joe Bowbeer joe.bowbeer at gmail.com
Sat Dec 5 02:25:42 EST 2009


Ken,

I'm imagining a serial executor per client:

ExecutorService pool = Executors.newCachedThreadPool();

Executor execA = new SerialExecutor(pool);
Executor execB = new SerialExecutor(pool);

SerialTask task1 = new SerialTask("client-a", 10);
SerialTask task2 = new SerialTask("client-a", 10);
SerialTask task3 = new SerialTask("client-a", 10);
SerialTask task4 = new SerialTask("client-b", 2000);
SerialTask task5 = new SerialTask("client-b", 4000);
SerialTask task6 = new SerialTask("client-a", 10);

execA.execute(task1);
execA.execute(task2);
execA.execute(task3);
execB.execute(task4);
execB.execute(task5);
execA.execute(task6);

--Joe

On Fri, Dec 4, 2009 at 9:51 PM, Ken-- at newsgroupstats.hk wrote:

>
> Dear Joe,
>
> Sorry I cannot under how to achieve it. I have tasks to be submitted in
> order (task1 to task6) like this:
>
> public class SerialTask implements Runnable {
>    private String client = null;
>    private long sleep = 0;
>    public SerialTask(String client, long sleep){
>        this.client = client;
>        this.sleep = sleep;
>    }
>    public void run() {
>        try {Thread.sleep(sleep);} catch (Exception e) {}
>        String msg = "Task ID = " + client + ", sleep = " + sleep;
>        System.out.println(msg);
>    }
> }
>
> SerialTask task1 = new SerialTask("client-a", 10);
> SerialTask task2 = new SerialTask("client-a", 10);
> SerialTask task3 = new SerialTask("client-a", 10);
> SerialTask task4 = new SerialTask("client-b", 2000);
> SerialTask task5 = new SerialTask("client-b", 4000);
> SerialTask task6 = new SerialTask("client-a", 10);
>
> How can I make use of SerialExecutor to execute client's task in order? I
> expected client-a's tasks can be finished in 40 mills and tasks are run in
> serial. and after around (2000 + 4000 - 40) mills, all client-b tasks are
> finished.
>
> Regards,
> Ken
>
>
>
>
> Joe Bowbeer wrote:
> >
> > If each client's tasks are executed sequentially, you can use a
> > SerialExecutor per client.  See the "SerialExecutor" example in the
> > Executor
> > javadoc.  Each client's executor would delegate to the shared thread pool
> > executor.
> >
> > If all tasks can execute in concurrently, but you want to retire them in
> > order, I think there was a useful discussion about this in June '08.
> > Search for "Out-of-order execution, in-order retirement".
> >
> > (The suggestion was to queue the Futures when the Callables are
> submitted,
> > and retire the tasks in the order they were enqueued.)
> >
> > --Joe
> >
> > On Wed, Dec 2, 2009 at 6:32 PM, Ken-- at newsgroupstats.hk wrote:
> >
> >>
> >> Hi All,
> >>
> >> I have a Task Queue (ArrayBlockingQueue) with single thread for client's
> >> task processing. For same client, I  have to guarantee the ordering of
> >> task
> >> results as I need the result of previous task to perform next task of
> the
> >> same client. It's work for ArrayBlockQueue with single thread as it's
> >> always
> >> FIFO.
> >>
> >> My question is, how to enable multi threads (like ThreadPoolExecutor)
> for
> >> queue but maintaining the ordering of same client's tasks? I tried
> >> PriorityBlockingQueue but it just ensure tasks are started in order.
> >>
> >> Any solution? Please help.
> >>
> >> Thanks and Best Regards,
> >> Ken
> >>
> >>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20091204/6c251c94/attachment.html>


More information about the Concurrency-interest mailing list