• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Observer/Observeable Channel

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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
task->engine->controller.

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
Tom
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic