[concurrency-interest] Regarding a design suggestion..

Peter Veentjer alarmnummer at gmail.com
Thu Mar 15 04:14:59 EDT 2007


Hi Rahesh,

I think I understand your pain. You need some kind of mechanism that
isn't passively waiting for something to execute (like an executor)
but is actively  waiting for something to execute.

I have created a threading structure called the Repeater that does
this job, example:

BlockingQueue inbound = new ArrayBlockingQueue();
BlockingQueue outbound = new ArrayBlockingQueue();

class BTask implements Runnable{
	public void run(){
		Object item = blockingQueue.take();
		Object result = item.process();
		outbound.put(result);
	}
}

class CTask implements Runnable{
	public void run(){
		Object item = outbound.take();
		take.someMoreProcessing();
	}
}

Repeater brepeater = new ThreadPoolRepeater(1);
brepeater.repeat(new BTask());
Repeater crepeater = new ThreadPoolRepeater(1);
crepeater.repeat(new CTask());

I don't have enough information for the 'A' step. It could be done
with an Executor when a task is actively placed, or done with a
Repeater if no task is being placed, but you need repeated execution.
If someone could place data directly in the inbound queue, the whole A
step can be removed.

On 3/15/07, Rajesh Balamohan <rajesh.balamohan at gmail.com> wrote:
> Any inputs on this would be of great help Dough.
>
>
>                 ============                 ============
>                 |          |                 |          |
>                 |          |                 |          |
> Insert Item =>  | Inbound  |                 | Outbound |   ===> out
>                 |  Queue   |                 |  Queue   |
>                 |          |                 |          |
>                 |          |                 |          |
>                 ============                 ============
>                     |                            |
>                     |                             |                |
>                     |                            |------------- |
>     1.    A seperate thread does                                  |
>         queue.take() from here                  1.    A seperate thread does
>     2. Creates a runnable task and                  queue.take() from
> outbound queue
>     enques to ThreadPoolExecutor (B)           2. Creates a runnable task
> and
>                                                 enques to
> ThreadPoolExecutor (C)
>
>
>
>
> Please let me know if you need more explanation.
>
> 1. There are 2 LinkedBlockingQueue. One for inbound and another for outbound
> 2. There are 3 ThreadPoolExecutors available. Letz say A, B, C
> 3. Someone puts a task in "A", which puts an item in InboundQueue.
> 4. A separate thread is blocked on "take()" method of this queue. Once it
> gets an item, it
> creates another "TASK" and puts it in "B" threadpool.
> 5. This task does some work and finally enqueues to "OUTBOUND QUEUE"
> 6. OUTBOUND Queue has a separate thread to dequeue and do some processing in
> "C" threadpool.
>
> Problem:
> ========
>  1. Too many short lived tasks are created.
> 2. Too many context switching happening in system due to #1.
> 3. Do you suggest any better way to implement it. Basic thing is the
> queueing and dequeuing has to happen asynchronously.
>
>
> --
> ~Rajesh.B
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>


More information about the Concurrency-interest mailing list