4 Replies Latest reply: Apr 13, 2013 2:35 AM by SanchitJavaDev RSS

    Performing sequential operations for groups of Parallel Threads

    SanchitJavaDev
      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?

      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.

      Edited by: 990484 on 26-Feb-2013 09:45