Question

I'm implementing a Java App which performs parallel processing: When my app starts, the 4 threads that belong to a Thread Pool are created. Then, I start receiving objects that are added to a LinkedBlockingQueue. Each object has an ID, and depending on the ID, a pre-defined task is performed. My "Queue Consumers" are those 4 Threads that were initialized, and they begin to take the objects out of the queue, and perform the tasks that corresponds to each object.

It is possible to define in which Thread each object will be processed? I want to "schedule tasks to each Thread".

For example:

  1. When the first object is taken from the queue, he will be processed by Thread1.
  2. If the second object as a different ID than the first one, it will be processed by Thread2.
  3. If the third object has the same ID than the first object that was taken, it will "go to Thread1".

How can I implement this?

Thanks

Was it helpful?

Solution

Let all threads have their own queue. You might implement a new thread like MainThread or DispatcherThread which takes objects from your LinkedBlockingQueue and decides which WorkerThread to process it. Something like :

private void decideQueue(String objectId) { // actually decides which thread to process it.
    Queue queue = getQueueIfObjectIdPreviouslyUsed(objectId);
    if (queue == null) {
        queue = getNextAvailableQueue();
    }

    queue.enqueue();

    //.. 
    //don't forget to dequeue from the main queue
    //..
}

To decide whether an onjectId is previously processed or not you might use a Map that keeps objectId as key and associated Queue as value. And you do the necessary work in getQueueIfObjectIdPreviouslyUsed method.

OTHER TIPS

Instead of one queue and a thread pool you could have a queue for each individual thread. Naturally, you would have to manage all this yourself or use a software package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top