title: Observer, Observable and Runnable. Why synchronized block lost monitor?
body: <p>I'm trying to use class as Observer and Observable. This class will be runned as thread. In run() method thread will be waiting and after getting event thread will be notifyed. There is example code:</p>
  
  <pre><code>public class Runner {
  
      public static void main(String[] args) {
          MyThread mt = new MyThread();
          Controller c = new Controller();
          mt.addObserver(c);
          c.addObserver(mt);
          Thread t = new Thread(mt);
          t.start();
      }
  
  }
  
  
  public class MyThread extends Observable implements Observer, Runnable {
  
      static private Integer op = 0;
  
      public void run() {
        synchronized (this) {
          while (true) {
            op++;
            System.out.println(op + " Thread started");
            super.setChanged();
            super.notifyObservers(new Object());
            op++;
            System.out.println(op + " Thread send event");
            try {
              op++;
              System.out.println(op + " Thread wait");
              this.wait();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
      }
  
      @Override
      public void update(Observable arg0, Object arg1) {
        op++;
        System.out.println(op + " Thread got event");
        synchronized (this) {
          op++;
          System.out.println(op + " We are in synchronized block!");
          this.notify();
        }
      }
  
  }
  
  
  public class Controller extends Observable implements Observer {
  
    public void update(Observable arg0, Object arg1) {
      System.out.println("Controller get and send event");
      super.setChanged();
      super.notifyObservers(new Object());
    }
  
  }
  </code></pre>
  
  <p>Getted output is:</p>
  
  <pre><code>1 Thread started
  Controller get and send event
  2 Thread got event
  3 We are in synchronized block!
  4 Thread send event
  5 Thread wait
  </code></pre>
  
  <p>And thread stay locked. Expected output:</p>
  
  <pre><code>1 Thread started
  Controller get and send event
  2 Thread got event
  3 Thread send event
  4 Thread wait
  5 We are in synchronized block!
  </code></pre>
  
  <p>What's going wrong? Why I come into synchronized block before monitor released?
  P.S. I have an idea that problem is adding observer to MyThread object, may be i will add observer to Thread object? But how I could do this?</p>