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 7 of 7

Thread: Threads in some order...

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Threads in some order...

    Hi everyone,
    Well I am writing a program where there are two sets of threads. The first set has 4 threads and the second set has 6 threads. Now, in every round, I want the first set of threads to execute once in any order. Then the second set of threads to execute in any order. Only then should the program move on to the second round... E.g.
    ROUND = 1;
    First set of 4 threads run in any order.
    Second set of 6 threads run in any order.
    ROUND ++;
    First set of 4 threads run in any order.
    Second set of 6 threads run in any order.
    ROUND ++;
    First set of 4 threads run in any order.
    Second set of 6 threads run in any order.
    ROUND ++;
    First set of 4 threads run in any order.
    Second set of 6 threads run in any order.
    .......and this continues...
    I have tried to implement this, but I ended up using a monitor of my own and making a thread wait() until the monitor ID matches the thread ID. I have used while loops which makes the program inefficient and slow and it eliminates the whole purpose of using threads, i.e. to make the program efficient... Please help me out... How do I implement the process stated above? Thank you.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Threads in some order...

    There are quite possibly several ways to do this. One that comes to mind is to use an Observer design patter. See the following link:
    http://www.javaprogrammingforums.com...r-pattern.html
    In it, there is code that can be implemented to listen for when a thread has completed. In your case, you can create a ThreadListener which increments every time its threadFinished method is called (make sure the increment is synchronized in some way).

    For the first round, when that hits 4 you know all 4 threads are complete, in which case you can launch the second round and repeat with a new ThreadListener - which when its count reaches 6 you know all 6 threads have completed. You can then repeat in this style.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Threads in some order...

    You could accomplish this by using a kind of semaphore, basically an Object that the threads can wait and notify on. The Object should contain two ints, and two locking Objects, one for each thread group. As each thread finishes, it locks on its locking Object and increments its variable. When the variable reaches the limit, it calls notifyAll() on the other thread group's locking Object, causing the other Thread group to start and the process to restart.

    Edit- Too slow. I didn't see copeg's post before I posted mine.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Threads in some order...

    Quote Originally Posted by copeg View Post
    There are quite possibly several ways to do this. One that comes to mind is to use an Observer design patter. See the following link:
    http://www.javaprogrammingforums.com...r-pattern.html
    In it, there is code that can be implemented to listen for when a thread has completed. In your case, you can create a ThreadListener which increments every time its threadFinished method is called (make sure the increment is synchronized in some way).

    For the first round, when that hits 4 you know all 4 threads are complete, in which case you can launch the second round and repeat with a new ThreadListener - which when its count reaches 6 you know all 6 threads have completed. You can then repeat in this style.
    Thanks, I'm going to try it now. Btw, "In which case you can launch the second round"-- I can't do that, because after the first set completes round one, I need the whole of the second set to complete round one as well... then only can the first set start with its round two. Will that need any extra care?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Threads in some order...

    I can't do that, because after the first set completes round one, I need the whole of the second set to complete round one as well... then only can the first set start with its round two. Will that need any extra care?
    I'm still not sure about the design based upon what you've given - it sounds like you want Round 2 to execute the code Round 1 has executed before going on to execute their own code? This shouldn't be a problem if you tread carefully - for example abstract your long running code into an interface (you could simply re-use the Runnable for this), then adapt the NotificationThread class in the link above to contain a List/Queue of Runnables which you can add (or remove) and which you can then call sequentially in the doRun method. In this way you can create 'compound' Runnables while still knowing when all code has executed using the ThreadListener notification.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Threads in some order...

    Quote Originally Posted by copeg View Post
    I'm still not sure about the design based upon what you've given - it sounds like you want Round 2 to execute the code Round 1 has executed before going on to execute their own code? This shouldn't be a problem if you tread carefully - for example abstract your long running code into an interface (you could simply re-use the Runnable for this), then adapt the NotificationThread class in the link above to contain a List/Queue of Runnables which you can add (or remove) and which you can then call sequentially in the doRun method. In this way you can create 'compound' Runnables while still knowing when all code has executed using the ThreadListener notification.
    Here is what my design is, in simple terms:-
    Let's say the names of the 4 threads first set are 1,2,3 and 4 respectively and the names of the 6 threads in the second set are A,B,C,D,E,F. The task of each thread in set one is to print "Hello I am a human and my id is <1/2/3/4>". The task of each thread in set two is to print "Hello I am a robot and my id is <A/B/C/D/E/F>". And they repeat the same tasks in every round. Here is a sample output:
    OUTPUT:-
    ROUND 1:
    Hello I am a human and my id is 2
    Hello I am a human and my id is 3
    Hello I am a human and my id is 4
    Hello I am a human and my id is 1
    Hello I am a robot and my id is B
    Hello I am a robot and my id is A
    Hello I am a robot and my id is C
    Hello I am a robot and my id is F
    Hello I am a robot and my id is D
    Hello I am a robot and my id is E

    ROUND 2:
    Hello I am a human and my id is 3
    Hello I am a human and my id is 1
    Hello I am a human and my id is 2
    Hello I am a human and my id is 4
    Hello I am a robot and my id is C
    Hello I am a robot and my id is A
    Hello I am a robot and my id is B
    Hello I am a robot and my id is E
    Hello I am a robot and my id is D
    Hello I am a robot and my id is F

    So, basically that's what I have to do, just that the tasks are a lot more complex than just print statements. I hope I have made myself clear. Thanks a lot for helping out.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Threads in some order...

    Nice description. Based upon that the original method I posted should work: note that if you need the state of each thread (I hesitate to say thread because the point here is to decouple what you run from the threading itself) to remain after it runs for the next round, the last method I posted would suffice and be quite flexible. Give them a try and post back if you run into problems.

Similar Threads

  1. Arranging words in a sentence in alphabatical order.
    By J2000 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2011, 10:04 AM
  2. [SOLVED] JTables: How do I adjust row order as I drag columns?
    By assel in forum AWT / Java Swing
    Replies: 3
    Last Post: December 7th, 2010, 04:51 PM
  3. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM
  4. Sort in Cyrilic order
    By cselic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2010, 03:08 PM
  5. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM