File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Performing sequential operations for groups of Parallel Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Performing sequential operations for groups of Parallel Threads" Watch "Performing sequential operations for groups of Parallel Threads" New topic

Performing sequential operations for groups of Parallel Threads

sanchit dua
Ranch Hand

Joined: Dec 05, 2011
Posts: 31
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:

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

Joined: Dec 05, 2011
Posts: 31
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.
I agree. Here's the link:
subject: Performing sequential operations for groups of Parallel Threads
It's not a secret anymore!