?
Solved

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

Posted on 2006-07-07
11
Medium Priority
?
2,089 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:booksplus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 672 total points
ID: 17060314

if you have     A a;

->        a.join();  wait this thread

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17060322
If you're implementing Runnable, you need to create a Thread :

Thread th=new Thread( a );
th.start();
th.join();
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 664 total points
ID: 17061290
> 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??

When B calls join on A, the execution of B will be paused until A completes (NOT other threads)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:astorer
ID: 17065904
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


0
 

Author Comment

by:booksplus
ID: 17075670
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.

0
 
LVL 4

Assisted Solution

by:astorer
astorer earned 664 total points
ID: 17077087

// Build a couple of lists of arrays and objects.
List threads = new ArrayList();
List objects= new ArrayList();
for (int i=0; i<4; i++){
  YourClass obj = new YourClass ();
  Thread t = new Thread(obj);
  t.start;
  threads.add(t);
  objects.add(obj);
}
// All your objects now running in their own threads.
// Loop until all threads have stopped have stopped...
int runningThreads = 0;
while (true){
  runningThreads = 0;
  for (int i=0; i< threads.size(); i++){
    Thread t = (Thread)(threads.get(i));
    if (t.isAlive())
      runningThreads ++;
  }
  if (runningThreads== 0)
    break;
// maybe put a little delay here so loop not too tight...
}
// Now all threads have completed
 for (int i=0; i< objects.size(); i++){
    YourClass obj = (YourClass )(objects.get(i));
    Results[] resutls = obj.getResults();
}

I'm sure there are more elegant ways to achieve this - but the above basically works.

One option is to have YourClass simply set a boolean and then test isFinished() and the List of objects.  That would save having to check the Thread.isActive().

Andrew
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17079831
>>    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.
0
 

Author Comment

by:booksplus
ID: 17083616
Would I actually call the .run method instead of .start()   --  I'll want to start multiple threads.

Please elaborate.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17083777
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)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question