• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Thread Callbacks

Hi,
  Right, I've got my objects and they're all threading and running nicely - only I now have another problem :)

  I run three threads and the next stage of the application is to combine the results of those threads. This is posing a problem because I'd first need to know if all three threads have completed before beginning stage two. I thought that I could "register" the threads in an ArrayList or somesuch and when complete, get the run method to remove themselves from the list. Thus when the list is empty, I know the results are all ready.

  The only problem with that is that I'd need a while loop to keep on checking :


while((array.size()==0)){

//Do Stuff


}


I was wondering if there was a better less intensive way of doing this? I imagine there is - but my googling and tutorial reading hasn't found it yet.... although I'm still looking ;)


0
petepalmer
Asked:
petepalmer
  • 8
  • 5
  • 2
  • +1
2 Solutions
 
CEHJCommented:
Your question title gives the clue - use a callback method (synchronized of course)
0
 
girionisCommented:
What about having a flag that indicates what thread finishes. Lets say that you have 5 threads, then you can define an integer for example, synchronize it and increment it everytime a thread finishes. Then you check it every time to see if it has the value of 5. If it does then it means that all threads have finished processing and you can go on otherwise wait till all of them finish.
0
 
CEHJCommented:
class DataStorer implements Runnable {
      private ClassToCallback c;
      
      public DataStorer(ClassToCallback c) {
            this.c = c;
      }
      
      public void run() {
            // Process, then ...
            c.aggregate(this);
      }    
}

.....


public ClassToCallback {
      public synchronized void aggregate(DataStorer ds) {
            // Aggregate results
      }
}
0
Industry Leaders: 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!

 
CEHJCommented:
>>public ClassToCallback

should have been

public class ClassToCallback
0
 
cjjcliffordCommented:
easy way to wait for several threads is to join() each one one after the other, then when all threads complete, the join() method will return.

example:

Thread thread1 = new Thread( new Runnable() { public void run() { Thread.sleep( 1000 ); } } );
Thread thread2 = new Thread( new Runnable() { public void run() { Thread.sleep( 2000 ); } } );
Thread thread3 = new Thread( new Runnable() { public void run() { Thread.sleep( 3000 ); } } );

thread1.start();
thread2.start();
thread3.start();

thread1.join();
thread2.join();
thread3.join();

System.out.println( "All 3 threads are complete now" );

0
 
petepalmerAuthor Commented:
so join basically blocks until the thread returns?

0
 
cjjcliffordCommented:
I'm assuming you mean when the thread completes (i.e. when the run() method returns) - basically yes, but calling join() on a thread that is already completed just returns, so from my example, even if thread2 or thread3 complete before thread1 (which won't happen in my example, I put the sleeps in the wrong order...) making the calls in this order will work correctly.
0
 
petepalmerAuthor Commented:
so in this example, thread2 can't return until thread1 completes..... but if thread2 has already completed, when thread1 completes, thread2 will instantly return?

0
 
CEHJCommented:
You should let all threads run freely. Calling join on a thread will make the calling thread wait for it to complete. That's unnecessary and undesirable
0
 
petepalmerAuthor Commented:
In this case though, I don't want the main thread to continue until the other three have completed. Wouldn't join() be the right solution in such a case?
0
 
CEHJCommented:
>>Wouldn't join() be the right solution in such a case?

Yes. A callback is therefore not suitable

0
 
petepalmerAuthor Commented:
So, use a call back when you want them to run independantly and let you know when they finish and use join when you want to wait for a thread or threads to complete....
0
 
CEHJCommented:
You could say that. A better way to think of it is that you must use join if the calling thread must wait before proceeding with any other processing
0
 
petepalmerAuthor Commented:
That is a better way of putting it :)
0
 
CEHJCommented:
If you had a gui app that showed aggregated result sets, you could use callbacks there. The display could be refreshed each time new results came in
0
 
CEHJCommented:
8-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now