0

I have the following scenario which needs to be changed as soon as possible:
1. A group of sequential threads executes (contains 3 threads)
2. A group of another sequential threads executes (contains 2 threads)
3. A group of parallel threads executes (contains 9 threads such that producer-consumer approach of threads is applied correctly)

Why I created this scenario?

To execute a particular threads first (point-1: containing 3 threads), after their processing is done i required to execute another 2 threads (point-2: containing 2 threads). If and only if the 1 and 2 points are covered I required to process point-3.

Initially, I required to execute all the threads to process parallell operations, but I left that idea as the parallel threads would execute simultaneusly.

For the above situation everything is working smooth until some problem occured.

Why I need to change the above scenario?

Sometimes, I get an Exception which can be handled well in case of parallel threads. While the same Exception if I get in the Sequential threads that becomes unable to handle. Since, all other processing of sequential threads comes under waiting condition until the first completes.


So, I need to take the benefit of parallel threads but I chosen this way to be easy which has become a tough to handle situation for the application.
So, I need to create such a scenario in which I should execute different parallel threads for a fixed order. E.g:

Order-1st = process the first 3 parallel threads
Order-2nd = process the next 2 parallel threads
Order-3rd = process the next 9 parallel threads

Also, the above case is somewhat restricted, though I need a generic solution for this. Like if in future I want to add 2 more threads from Order-1 or, if I even remove some threads for any order the sequence of the groups should be executing as desired.
Is there any way in java through which we can make a group of parallel threads such that they can be executed for whatever order we may specify?

Edit
I have the following code snippet if you can help me modifying:

// Level-1

Thread[] threads = new Thread[list1.size()];
int j=0;
for(list1.size()){
    Object delegator1 = new Object("Level-1");
    Thread th = new Thread(delegator1);
    threads[j]=th;
    th.start();

    j++;
}
for(Thread thread: threads){
    thread.join();
}

// Level-2

threads = new Thread[list2.size()];
j=0;
for(list2.size()){
    Object delegator2 = new Object("Level-2");
    Thread th = new Thread(delegator2);
    threads[j]=th;
    th.start();

    j++;
}
for(Thread thread: threads){
    thread.join();
}

// Level-3 & 4 --> Applied Producer-Consumer using wait-notify working fine.

for(list3.size()){
    Object delegator3 = new Object("Level-3 & 4")
    Thread th = new Thread(delegator3);
    th.start();
}

I want these different levels to execute parallelly but if first level finishes off the second level should thereafter executes.

3
  • Did you try with multilevel producer consumer ? I mean Order-1 will be producer for Order-2 and Order-2 will be consumer for Order-3 group of threads.
    – Santosh
    Feb 26, 2013 at 7:36
  • No I didn't try the multilevel producer consumer. Since in that way the number of threads executing will become restricted. Means, If in future I need to add around 50 more theads to Level-1 then the code change should not be done. If we can handle without any code change, then please help me out maintaining this hierarchy.
    – Sanchit
    Feb 26, 2013 at 7:48
  • Not really, you can always keep the number of threads configurable through some property file. The application will read the configuration at start-up.
    – Santosh
    Feb 26, 2013 at 8:45

2 Answers 2

1

So that looks like you have some states for your Order-Instances. The states are Order-1, Order-2, Order-3 and Failed.

You can use Producer-Consumer-Queues to implement those states (or even a single queue for all). Depending on the result of your operation the instances are moved to the 'next' queue which is processed by the amount of threads you need.

When using a single queue, a possible way to process it could be a Envelope-Object that contains the computation, like

class Order1Processor implements Runnable {
     Object delegate;
     Order1Processor(Object delegate) { this.delegate = delegate; }
     public void run() { 
       try {
          // do the Order-1st processing
          doSomethingWith(delegate);
          // submit to next free Thread for Order-2nd processing
          submitToQueue(new Order2Processor(delegate));
       } catch (Throwable t) {
          // signal/handle failure
          submitToQueue(new FailedProcessor(delegate, t))
       }
 }


}  
class Order2Processor .. 

you can now use as many threads as you like to process your orders. A single queue allows you to assign as many ressources as needed automatically, depending on the current work to do. You can even use a PriorityQueue if you have some Orders or Operations on them that have to be processed earlier or in a certain sequence.

1
  • ok, it seems to be doable. I will let you know if any problem processing the above workaround happens.
    – Sanchit
    Feb 26, 2013 at 8:24
0

If you just want to wait for a set of threads to finish, you can use for example a CountDownLatch in the main thread, initialized to the amount of threads. The main thread then waits for each thread to finish and notify the latch.

Repeat for the next sets.

Which ultimately solved my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.