[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2006-07-07
11
Medium Priority
?
2,151 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

649 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