Dirst of all, you do not want to go create objects inside a loop.
Second, you need to have the Thread instance availlable outside of the loop to be able to call any methods on it.

Originally Posted by
yogeshseth
Code:
AnObject obj = new AnObject();
for(int i =0;i<someInt;i++) {
ATarget aTarget = new ATarget(obj);
Thread t = new Thread(aTarget);
t.start();
}
obj.waitForAllThreadsAndDoSomething();
should be:
Code:
AnObject obj = new AnObject();
Thread[] ta = new Thread[someInt];
for(int i =0;i<someInt;i++) {
ATarget aTarget = new ATarget(obj);
Thread t = new Thread(aTarget);
t.start();
ta[i]=t;
}
// now we can access the actual threads:
for(int i =0;i<someInt;i++) {
ta[i].join();
}
the join method makes the parent thread wait on the child threads.
In the last method call, i want to do something which requires all the threads to be finished processing. One brute force approach is to implement the method waitForAllThreadsAndDoSomething() in the following manner:
Code:
while(true) {
if(aCounter >= childrenThreadsCount) {
//do something
}
}
but this is too ugly and memory consuming.
can anyone suggest a cleaner approach by using methods in thread class like: wait(), notify(), yeild() ?
cheers[/QUOTE]
Bookmarks