Forums Register Login

Thread Question

+Pie Number of slices to send: Send
I have a main application, which create a number of Threads, of objects that implement the Runnable Interface, to calculate certain values.

Is there an appropriate way of getting the main application to receive results from the Threads when they have completed?

I was thinking of firing an Event at the end of the run() method - but im not sure how to do this
+Pie Number of slices to send: Send
As far as I read through your requriement, there will be no need for event firing stuff in such application... Could you provide us ur real code so that people out here can help you more to fit your requirement? If not, there may be doubt and we cannot help you as possible as we can...

Looking forward to see your code soon..
+Pie Number of slices to send: Send
There are many ways to do it, but perhaps the easiest way is to provide a "callback object" when you create the Runnables. Some place in your program -- i.e., in the class that starts all the threads -- define a method called "reportResults()" or something like that -- of course, the method should take an argument appropriate to the type of results you're computing. Then when you create each Runnable, pass "this" (or more generally, a reference to the object with the "reportResults()" method) as a constructor argument. The Runnable should store this reference as a member variable. When each Runnable completes, it should call reportResults() to... well, report its results.

The method reportResults() should almost certainly be synchronized!
+Pie Number of slices to send: Send
I have a number of Servers (for the moment 2) which have been given a task (for the moment it is just generate a random number) - I have an application that generates the Runnables which make the connect to the servers etc and generate the number. The application is not a Runnable object.

I want all the threads to send their result back to the application when they have finished. And thinking about it further - Im thinking that:

1) the application needs to know when each thread has completed.
2) the application needs to know when all of the threads have completed so that it can display the results.

- I think as Ernest Friedman-Hill suggested that if the application has an array of objects and each thread has a reference to an object - then the application only needs to know when all of the threads have completed to display the results.

Hope that makes it easier
+Pie Number of slices to send: Send
You can use the method Thread.join() to wait for a Thread to complete; given that array of Threads, the standard idiom is just to do

+Pie Number of slices to send: Send
I just this second tried to group my threads - which would make sense and then loop until the activeCount() of the group =0; until i display the results. Seems to have worked. I shall try using join() now.


Thanks for all the help
[ July 01, 2004: Message edited by: Tom Hill ]
+Pie Number of slices to send: Send
I just want to add somethig to respond about "call back" methods.

If you pass "this" reference, make sure you dont do it from the constructor of this object. Otherwise your threads may get reference to not fully constructed object and application will be unstable.
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 661 times.
Similar Threads
Individual Thread Definitions
Daemon Thread
main app won't stop when all threads are done running
what is a Daemon Thread
Exiting a multi-threaded Swing application without System.exit
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:58:03.