Except wait(), other methods such as notify(), join(), sleep(), yield()keep locks with them when they go to Non-Runnable(also referred to as Waiting/blocked/sleeping state). If these statements are true than the below program should print 121212
but its printing 111222. Anybody can explain why?
----------------------------------------------------------
public class Synch implements Runnable {
synchronized void get() {
System.out.println(1);
try {
Thread.sleep(600);//This should keep lock on the object
} catch (InterruptedException ie) {
}
System.out.println(2);
}
public void run() {
this.get();
}
public static void main(
String as[]) {
Synch s = new Synch();
Synch s1 = new Synch();
Synch s2 = new Synch();
Thread t = new Thread(s);
Thread t1 = new Thread(s1);
Thread t2 = new Thread(s2);
t.start();
t1.start();
t2.start();
}
}