Solved

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

Posted on 2006-07-07
11
1,912 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
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 168 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 166 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 166 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 50
GUI builder for Eclipse? 8 27
more than one jdk and one jre 1 32
tomcat not starting 6 30
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now