Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread to notify Object that created it?

 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a thread that checks for a certain file to appear in a folder. I need this thread to notify the object it was created in that the file was found. Can I fire an event from the thread? Or is there a better way of doing this. If I can fire an event from a thread, can you explain how to do that or point me to where I can learn it.
Thanks,
Warren Bell
warren@netricks.net
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Warren Bell:
I have a thread that checks for a certain file to appear in a folder. I need this thread to notify the object it was created in that the file was found. Can I fire an event from the thread? Or is there a better way of doing this. If I can fire an event from a thread, can you explain how to do that or point me to where I can learn it.
Thanks,
Warren Bell
warren@netricks.net

The Observer pattern would be a good fit here. Take a look at java.util.Observer and java.util.Observable. The thread object would need to be the Observable and since Observable is a class that means that you would need to extend Observable and implement Runnable instead of extending Thread. The Observer interface has a single callback method:


You must register your interest in the Observable by calling:


Whenever your Runnable finds the file it would then call:

That will cause the update() method to be invoked on all registered Observers.
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a good solution, but I have one problem, I can not implement Observer do to limitations of the IDE I am using. But I think I have a solution though, see what you think. I could make my thread an inner class of the class I want notified by the thread. When the thread finds the file, it would call a method of the outer class. Do you see any problems with this?
Thanks,
Warren Bell
warren@netricks.net
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Warren Bell:
Sounds like a good solution, but I have one problem, I can not implement Observer do to limitations of the IDE I am using. But I think I have a solution though, see what you think. I could make my thread an inner class of the class I want notified by the thread. When the thread finds the file, it would call a method of the outer class. Do you see any problems with this?
Thanks,
Warren Bell
warren@netricks.net

That should work. You may need to synchronize the method or any affected data if more than one thread has access to the method.
 
Nathan Paris
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that you dont need to user java.util.Observable to follow the pattern. I would stick with the pattern and just make your own classes. I.e. I would make an interface (Observer) that could be passed in to recieve the updates form the thread.
I.e.:

By creating the listener (Observer) from an interface you will only expose what should be exposed instead of the entire object which will make your designed less coupled. It will also make unit testing a lot easier, because you can create a Mock listener from the listener interface and be able to test without having to rely on the other object as well.
Just some food for thought.
Nathan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic