• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Question  RSS feed

 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!