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.
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.
Re: Threads in some order...
Quote:
Originally Posted by
copeg
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?
Re: Threads in some order...
Quote:
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.
Re: Threads in some order...
Quote:
Originally Posted by
copeg
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.
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.