title: Observer Pattern Java - Multiple Observers Using Threads
body: <p>I am trying to learn all major design patterns. I have just started to learn multithreading in java. This is observer pattern code where there are multiple observers &amp; the observable is running in a while true loop. I'm having the following two problems:</p>
  
  <ol>
  <li>When calling notifyObservers, I have to pass the new value to it even though my observers are accessing the getter of the observable value. If I don't, then the observer gets/prints value null. I have created a setter function to demonstrate it.</li>
  <li><p>I created two observers &amp; obviously most of the code for both of them is similar. I'm not sure how to implement new observers or create a list of observers without replicating my code. I created a basicObserver &amp; tried to inherit it in MyObserver3, but I am not sure how exactly do I implement it.</p>
  
  <pre><code>import java.util.Observable;
  import java.util.Observer;
  
  public class ObserverPatternMultipleObserversUsingThreads
  {
      public static void main(String[] args)
      {
          ObservableValue observableObj = new ObservableValue(10);
  
          MyObserver1 observer1 = new MyObserver1(observableObj);
          MyObserver2 observer2 = new MyObserver2(observableObj);
          MyObserver3 observer3 = new MyObserver3(observableObj);
  
          observableObj.addObserver(observer1);
          observableObj.addObserver(observer2);
  
          observableObj.start();
  
          System.out.println("Calling Listeners");
  
          observer1.printObservablesValue();
          observer2.printObservablesValue();
  
          System.out.println("Main thread says: Sleeping for 3 second(s)");
  
          try
          {
              Thread.sleep(3000);
          }
  
          catch (InterruptedException e)
          {
              e.printStackTrace();
          }
  
          System.out.println("Main thread says: Going to change Observables Value");
  
          observableObj.setValue(20);
      }
  }
  
  class basicObserver
  {
      private ObservableValue obsValObj = null;
  
      public basicObserver(ObservableValue obsVal)
      {
          this.obsValObj = obsVal;
      }
  
      public void printObservablesValue()
      {
          System.out.println("Observer says: [" + obsValObj.getValue() + "]");    
      }
  
      public void update(Observable o, Object arg)
      {
          Integer count = (Integer) arg;
          System.out.println("Observer says: Value changed to: [" + count + "]");
      }
  }
  
  class MyObserver3 extends basicObserver implements Observer
  {
      public MyObserver3(ObservableValue obsVal)
      {
          super(obsVal);
      }
  }
  
  class MyObserver1 implements Observer
  {
      private ObservableValue obsValObj = null;
  
      public MyObserver1(ObservableValue obsVal)
      {
          this.obsValObj = obsVal;
      }
  
      public void printObservablesValue()
      {
          System.out.println("MyObserver1 says: [" + obsValObj.getValue() + "]"); 
      }
  
      public void update(Observable o, Object arg)
      {
          Integer count = (Integer) arg;
          System.out.println("MyObserver1 says: Value changed to: [" + count + "]");
      }
  }
  
  class MyObserver2 implements Observer
  {
      private ObservableValue obsValObj = null;
  
      public MyObserver2(ObservableValue obsVal)
      {
          this.obsValObj = obsVal;
      }
  
      public void printObservablesValue()
      {
          System.out.println("MyObserver2 says: [" + obsValObj.getValue() + "]"); 
      }
  
      public void update(Observable o, Object arg)
      {
          Integer count = (Integer) arg;
          System.out.println("MyObserver2 says: Value changed to: [" + count + "]");
      }
  }
  
  class ObservableValue extends Observable implements Runnable
  {
      private int n = 0;
  
      public ObservableValue(int x)
      {
          this.n = x;
      }
  
      public int getValue()
      {
          return n;
      }
  
      public void setValue(int x)
      {
          this.n = x;
          setChanged();
          System.out.println("ObservableValue says: setChanged() has been called");
  
  //      notifyObservers(new Integer(this.n));
          notifyObservers();                                                                  // makes the observers print null
          System.out.println("ObservableValue says: notifyObservers() has been called");
      }
  
      public void start()
      {
          new Thread(this).start();
      }
  
      public void run()
      {
          int count = -1;
          int a=0, b=0;
  
          while(a==b)
          {
              if(count != n)
              {
                  count = n;
                  System.out.println("ObservableValue says: My count is: [" + count + "]");
  
                  count++;
                  System.out.println("ObservableValue says: Now my count is: [" + count + "]");
                  setChanged();
                  System.out.println("ObservableValue says: setChanged() has been called");
  
                  notifyObservers(new Integer(count));
                  System.out.println("ObservableValue says: notifyObservers() has been called");
  
                  System.out.println("ObservableValue says: Sleeping for 5 second(s)");
  
                  try
                  {
                      Thread.sleep(5000);
                  }
  
                  catch (InterruptedException e) { e.printStackTrace(); }
              }
          }
      }
  }
  </code></pre></li>
  </ol>
  
  <p>I would really appreciate any pointers/advice/comments regarding best practices with these concepts.</p>
  
  <p>Please help.</p>