File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Observer/Observeable Channel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Observer/Observeable Channel" Watch "Observer/Observeable Channel" New topic

Observer/Observeable Channel

Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Im using this channel to get notification from 2 different sources (call them "engines 1 and 2"), which update the same observer/"controller". Each of the 2 engines is an observer itself also, of worker threads (call them "tasks"). Hence, my notification filter from the

An example would be an application comparing 2 sets files against each other and writing the differences between 2 files to a new file. Hence engine1 would be the ComparatorEngine and engine2 would be the FileWriterEngine. There would be multiple sets of files being compared, so the ComparatorEngine observers many "CompareTasks" while the FileWriterEngine observers many FileWriterTasks.

This is the flow :
The controller creates both engines. The controller is informed (by a another object) that it needs to start a CompareTask. It does so using ComparatorEngine and then waits. When controller gets the notifications from ComparatorEngine that the task is done, controller then creates a FileWriterTask using FileWriterEngine. When controller gets the notifications from FileWriterEngine that the task is done it decrements a filesToProcess counter (or something similar).
There can up to 5 CompareTasks and 5 FileWriterTasks running in parallel, so 10 tasks in total.

Im running this on windows and unix and seem to be getting different behaviour. eg When i call

on windows from the FileWriterEngine, sometimes (but not always) the observer's update() method never gets called. It is like im missing the notification. Usually its only for one task but it has happpened where 2 have never caused the update method in observer to be called.
So application hangs waiting for this. It doesnt seem to happen running on solaris server. Is it possible im overloading the observerable channel on windows or could the threads be interfering with each other? I find it strange though that i can see the setChange() & notifyObservers() being called using a 'println' but the update just never happens.

Thanks for any help

<a href="" target="_blank" rel="nofollow">Use Code Tags!!</a>
Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 94
Without some example code to see (which I guess could be lengthy), my first guess is that something involving thread collision is happening. Have you tried to make the notification method synchronized, or the block where the notification is called synchronized?
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Yes i tried synching the update method of the observer but it didnt seem to make any difference. I would have thought that it wouldnt be required as then at least all updates would enter the update method?

Here is the update method :

Nothing special, just delegate the handling of each update to its appropriate method. There is some synching in each of these methods when queueing tasks etc but this update method is not synched so i would expect that all notification would get into it anyway.

Here is the FileWriterEngine code

Here is the task run method

Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 94
Actually, I was referring to synchronizing the method that notifies the observers. I am assuming that the update( ) method is called when an observer is notified. Synchronizing update shouldn't make any difference, because you are reporting that sometimes it isn't even called, which leads me to question whether the notification is somehow getting hosed by another thread.

Like this:

If that doesn't work, I would plan to log every entrance into the notification method and dump the list of observers that should be notified. I can imagine several possible points of failure, including using an iterator incorrectly when going through the list, modifications made to the list "behind the scenes" causing the iterator to be out of synch with the list, etc.

Good luck, let us know if you find it.
I agree. Here's the link:
subject: Observer/Observeable Channel
Similar Threads
wait/notifyAll() a bit like addObserver(this) and notifyObservers() ?
Playing with Set and List
Question Regarding MVC Approach
FBNX: Gui and Threads, asynchronous Swing action execution and GUI updating.