In your example the wait() method releases lock on the AThread object.
Look at this example :
public class MyThread extends Thread {
boolean isRunnable = true;
public MyThread() {
start();
}
public synchronized void run() {
System.out.println("In run method");
while ( isRunnable ) {
try {
wait();
}
catch ( InterruptedException ie ) {
}
}
}
public static void main (
String[] args ) {
MyThread firstThread = new MyThread();
try {
sleep(1000);
}
catch ( InterruptedException ie ) {}
OtherClass oClass = new OtherClass();
oClass.printString("Hello", firstThread);
}
}
class OtherClass {
public void printString(String s, Object o ) {
synchronized(o) {
System.out.println(s);
}
}
}
Output of the above example is
In run method
Hello
The run method releases the lock on MyThread object when wait() is executed. When oClass object runs it's printString() method, it tries to obtain the lock on MyThread object and it gets it. Hence it prints "Hello". If you remove the wait() from MyThread in the above example, then only "In run method" is called since oclass's printString() does not get lock on MyThread.