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..
Ernest Friedman-Hill
,
author and iconoclast
staff
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!
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
Ernest Friedman-Hill
,
author and iconoclast
staff
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 ]
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.
Post by:autobot
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