Setting a breakpoint and stepping through/debugging the application will help you find the cause for this behaviour.
The reason is that MyThread.update
is called before the thread starts waiting, and there is no other thread to wake this thread. You would require a second thread.
In the MyThread.run
method you are notifying the Controller
object with this line:
super.notifyObservers(new Object());
This calls the update
method of the Controller
object, which in turn then calls the update
method of the MyThread
object (by notifying it) which prints the synchronised block message.
Then the notifyObservers
call in your MyThread.run
returns and only then do you reach your call to the wait
method.
To reach your expected result, you would need a second thread to notify your MyThread
object after you have called wait.
The simplest example using the main thread requires these changes:
Remove notification in Controller.update
:
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());
}
}
Add notification after starting MyThread
instead, this is called from the main thread.
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();
//add the following:
try {
Thread.sleep(1000); //sleep for a while to make sure MyThread is waiting
} catch (InterruptedException ex) {
Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
}
c.notifyObservers(); //notify MyThread
}
This will yield the following result:
1 Thread started
Controller get and send event
2 Thread send event
3 Thread wait
4 Thread got event
5 We are in synchronized block!
6 Thread started
Controller get and send event
7 Thread send event
8 Thread wait
As you can see, MyThread.run
continues after it gets the notification