Thread question: while(threadGroup.activeCount()!=0)
ThreadGroup threadGroup =new ThreadGroup("some group");
//there are 5 units fyi
for (String aunit: units){
Thread thread =new Thread(threadGroup,new someobject(...));
thread.start();
}
while(threadGroup.activeCount()!=0){
}
//do some stuff here
return ...;
i am confused about the while(threadGroup.activeCount()!=0) part...
once the first thread starts, shouldn't it be stuck in that infinite loop because it's the active thread ? how is the code below it ever going to execute? This is existing code that i'm trying to understand from a previous programmer. Thanks.
[1037 byte] By [
lapcherna] at [2007-11-27 1:40:41]

While that loop is spinning (which is a BAD idea to do by the way, so please feel free to smack the developer of that CPU-burning code upside the head with a dead mackerel!), the threads in that thread group are doing their thing and finishing their work, thus making the activeCount() return different values until finally all the threads are dead.
ok im trying to refreshen my java and i just realized that the run method of that object will be called and once it's done finishing its run method, the thread will terminate, thereby allowing it to run the '//do some stuff' here part.
> While that loop is spinning (which is a BAD idea to
> do by the way, so please feel free to smack the
> developer of that CPU-burning code upside the head
> with a dead mackerel!), the threads in that thread
> group are doing their thing and finishing their work,
> thus making the activeCount() return different values
> until finally all the threads are dead.
thanks for the extremely quick response. do you have any pointers or direction on how to optimize this solution? i am pretty new to threads so any direction would be great. should i start looking into semaphores n such? thanks.
Invoke Thread.join() on each thread created. You'll need to keep a collection of threads while creating and starting them, instead of throwing away the references of course.
ah ok so something like
ThreadGroup threadGroup = new ThreadGroup("some group");
Thread thread;
//there are 5 units fyi
for (String aunit: units) {
thread = new Thread(threadGroup, new someobject(...));
thread.start();
}
// 5 threads should be running
thread.join();
//do some stuff here
return ...;
thanks again
Message was edited by:
lapchern
> ah ok so something like
>
>
>
> ThreadGroup threadGroup = new ThreadGroup("some
> me group");
>Thread thread;
> //there are 5 units fyi
> for (String aunit: units) {
> thread = new Thread(threadGroup, new
> new someobject(...));
> thread.start();
> }
>
> thread.join();
>
> //do some stuff here
> return ...;
>
>
> thanks again
No, you're still throwing away all the references except the very last one. All that code would do would be to join() the last thread, not all of them unless there were only one.
ThreadGroup threadGroup = new ThreadGroup("some group");
Thread threads[] = new theThread[5];
//there are 5 units fyi
int i = 0;
for (String aunit: units) {
threads[i] = new Thread(threadGroup, new someobject(...));
threads[i].start();
i++;
}
for(int j < 0; j < threads.length; j++)
threads[j].join();
//do some stuff here
return ...;
not the most elegant way it seems. i guess i would do away with the whole ThreadGroup as I can't understand why I would need it anymore.
> not the most elegant way it seems.
I don't see anything wrong with it. However there's probably a pre-built solution in Java 5's new java.util.concurrent package -- I haven't played with that so I don't know for sure.
> i guess i would do
> away with the whole ThreadGroup as I can't understand
> why I would need it anymore.
Yup.