This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Thread to notify Object that created it?  RSS feed

 
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
 
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.
 
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
 
Paper beats rock. Scissors beats tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!