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


LVL 1
petepalmerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.