Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8056977

Observable/Observer pattern may miss dispatching events

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 7u67
    • core-libs

      FULL PRODUCT VERSION :


      ADDITIONAL OS VERSION INFORMATION :
      True on all platforms

      A DESCRIPTION OF THE PROBLEM :
      The current pattern when submitting and event using the Observable/Observer classes is as follows:

      1. setChanged
      2. notifyObservers

      The setChanged is required to set the variable 'changed' to true and notifyObservers will only dispatch events if 'changed' is set to true, if not it will return.

      So, if 2 threads are doing that sequence:
      TH1: setChnaged
      TH2: setChanged
      Th2: notitifyObservers # will will dispatch event for thread 2 and then reset the 'changed' to false
      TH1: notitifyObservers # will return immediately because now 'changed' is set to false

      => Event for TH1 is never dispatched!

      So unless we add a synchronize block around both operations this is unreliable. And adding a synchronize block around both operations implies events are being dispatched with the lock held, which is precisely what the Observable code is trying to avoid.





      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      Add a synchronized block around:
      setChange
      notifyObservers

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: