Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Performing sequential operations for groups of Parallel Threads

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Performing sequential operations for groups of Parallel Threads

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Performing sequential operations for groups of Parallel Threads

    Where are you having problems?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Performing sequential operations for groups of Parallel Threads

    Thanks for informing, from the next time I 'll pay attention to posting the code.
    The application is executing fine for some contexts. But it becomes terrific in some situations that the automation no longer remains.
    The problem/Exception if I get in the level-1 and level-2, then the application freezes, which if get in level-3, the situation is handled.
    So, I need to change the application's behaviour from starting that it shouldn't rely on any of the sequential threads.
    I need to have a solution for the application to run in above manner (i.e level-1 processes then, level-2 and then level-3). But the threads for each level are required to be parallel.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Performing sequential operations for groups of Parallel Threads

    Can you post the code you are having problems with and something that shows the problem?
    Add a description of what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Performing sequential operations for groups of Parallel Threads

    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.

Similar Threads

  1. Reading from a sequential file to an array
    By Xrrak in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: August 20th, 2011, 01:33 PM
  2. Sequential Files!!!???
    By nitwit3 in forum Java Theory & Questions
    Replies: 7
    Last Post: July 27th, 2011, 08:22 AM
  3. [SOLVED] Performing Division with Double Variables
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 05:36 PM
  4. my FileReader(i think) isn't performing properly..
    By weej25aya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2011, 10:06 PM
  5. Replies: 0
    Last Post: February 23rd, 2010, 01:12 PM

Tags for this Thread