0

I've got a program where I initialize and start a variable number of threads. I need the main program to interact with the threads so I put those inside an infinite loop. After all the threads have died though I would like a way to break the loop. Here goes part of my code:

public static void main(String[] args) {
//some random initializing

for(int i = 1; i <= num; i++){     //p1 is a Runnable
        Thread t = new Thread(p1, "t"+ Integer.toString(i));
        t.start();
} 
while(true){
//do some stuff while threads are active
//not sure what if statement to put here to break loop
}

I can post more of my code if I need to, but I think it would be largely irrelevant.

Peter T.
  • 15
  • 1
  • 1
  • 4
  • you can check the answer here. http://stackoverflow.com/questions/5066160/java-threads-and-main-thread – Raghu Dec 05 '13 at 06:18
  • http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html – Franz Ebner Dec 05 '13 at 06:19
  • How about creating threadgroup and using method getActiveCount() ?? – Suresh Atta Dec 05 '13 at 06:23
  • The reason I didn't use a thread group is this is for a homework assignment on multi-threading. I am only suppose to use the basic .start(), .wait(), and .notify(). To organize my threads. – Peter T. Dec 05 '13 at 06:36

3 Answers3

3

you can use CountDownLatch(since java 1.5) here, tt is a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

Snippet:

CountDownLatch signal = new CountDownLatch(num);
...
for(int i = 1; i <= num; i++){     //p1 is a Runnable
        Thread t = new Thread(p1, "t"+ Integer.toString(i),signal);
        t.start();
} 
signal.await();           // wait for all to finish

... In run() Method of your Thread...

public void run(){
       ...                // do stuff
       signal.countDown(); // sending signal that work has done.
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • +1. Also, if you want to do something while waiting (as OP wants), you could go. `while (latch.getCount() > 0) { do stuff }` – user949300 Dec 05 '13 at 06:34
1

Instead of stalling with a while loop, just join all the threads. The join method essentially just waits for the thread to finish.

Thread[] threads = new Thread[num];
for(int i = 1; i <= num; i++){
    Thread t = new Thread(p1, "t"+ Integer.toString(i));
    threads[i] = t;
    t.start();
} 

for(int i = 1; i <= num; i++){
    threads[i].join();
}

For more info consult the docs

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
1

I would make an array of Threads, to check them later on if they are done, then go through the same for loop but also set the values in the threads array. Finally, in the while loop, I would check if all the threads are not done using the state method of the thread - How to check if thread is terminated?

Thread[] threads = new Thread[num];

for(int i = 1; i <= num; i++){     //p1 is a runnable.
        Thread t = new Thread(p1, "t"+ Integer.toString(i));
        threads[i-1] = t;
        t.start();
} 

while(!allThreadsCompleted(threads)) {
 // do some of your code here
}

public static boolean allThreadsCompleted(Thread[] threads) {
    for(int i = 0; i < threads.length; i++) {
        if(threads[i].getState()!=Thread.State.TERMINATED)
            return false;
    }
    return true;
}

I realize now that this would be useful if you actually want to do something in the while loop, not just for stalling purposes. Hope this helps.

Community
  • 1
  • 1
Vineet Kosaraju
  • 5,572
  • 2
  • 19
  • 21