Link to home
Start Free TrialLog in
Avatar of booksplus
booksplus

asked on

Class must implement runnable - how do I wait for threads to finish

I have a class "B" that "starts" a threads on a class "A" that implements runnable.

From what I understand if class "A" extends Thread then the class "B" could call join on "A" and
therefore would "wait" until all threads finished??

Is this right??

If so, is there somthing similar I can do in this case where class "A" implementes runnable (btw it must implement
because the "extends" is already used up on another class).

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Webstorm
Webstorm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Webstorm
Webstorm

If you're implementing Runnable, you need to create a Thread :

Thread th=new Thread( a );
th.start();
th.join();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Webstorm's answer is the answer to your question, but are you sure this is what you want to do?

 - You are in a Thread
 - you start another Thread  -  th.start()
 - you suspend the current Thread -  th.join()
 - the other Thread ends
 - your main Thread resumes

Why bother with other Threads as you seem to have only one Thread active at a time?
 The only reason I could see for that is if you are doing some processing after the th.start() but before the th.join().  That is processing that you wish to run in parallel with A.  If that is so, then it could be that the other Thread completes before you call join().  I'd suggest calling  th.isAlive() before calling join().

Andrew


Avatar of booksplus

ASKER

Just to refresh and make sure you understand what I'm doing...
Example.

loop 4 times
   obj.start();
end loop;

this starts 4 threads and at this point I want to wait for the threads to PAUSE HERE WHILE THE 4 THREADS FINISH MUCH WORK  (can't call join because obj is Runnable and extends another class).

next i want to call a method on obj, like this ...

Results[] resutls = obj.getResults();

and then proceed from there.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>    obj.start();
>> can't call join because obj is Runnable and extends another class
Then can't call start() too.

-> create new Thread for each Runnable, and call start(), join() on that Thread objects.
Would I actually call the .run method instead of .start()   --  I'll want to start multiple threads.

Please elaborate.
call of .run() doesn't run the thread - its just a method call.
If you want the multiple threads to do the same job, write a call that implements Runnable and create the thread using the this constructor: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable)