Solved

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

Posted on 2006-07-07
11
1,889 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HashMap Vs TreeMap 12 49
JAVA part two 5 41
Requested array size exceeds VM limit 3 48
backtracking recursion  code 19 40
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 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

21 Experts available now in Live!

Get 1:1 Help Now