Solved

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

Posted on 2006-07-07
11
1,951 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
varialbe initialization 11 37
Android development question 2 52
Facing this issue for maven proxy setting 2 19
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

791 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