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
Joined: Jan 30, 2002
Posts: 3451
posted
0
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.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Warren Bell
Ranch Hand
Joined: Dec 20, 2000
Posts: 55
posted
0
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
Joined: Jan 30, 2002
Posts: 3451
posted
0
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
Joined: Jun 21, 2000
Posts: 28
posted
0
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 unittesting 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