Link to home
Start Free TrialLog in
Avatar of petepalmer
petepalmer

asked on

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


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Your question title gives the clue - use a callback method (synchronized of course)
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.
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
      }
}
>>public ClassToCallback

should have been

public class ClassToCallback
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of petepalmer
petepalmer

ASKER

so join basically blocks until the thread returns?

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.
so in this example, thread2 can't return until thread1 completes..... but if thread2 has already completed, when thread1 completes, thread2 will instantly return?

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
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?
>>Wouldn't join() be the right solution in such a case?

Yes. A callback is therefore not suitable

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....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a better way of putting it :)
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
8-)