class MyThread extends Thread{
public void run(){
print();
}
synchronized public void print() {
// for(int i = 0; i< )
System.out.println("Thread : " + Thread.currentThread().getName());
try{
Thread.sleep(5000);}
catch(InterruptedException e ){
}
}
}
public class MyClass {
public static void main(String args[]) throws InterruptedException {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.setName("A");
t2.setName("B");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("EOM");
}
}
Output of the Program -
ThreadA
ThreadB
Immediately prints both line After 5 seconds
EOM
According to my understanding, one of the thread should go in print() and acquire lock and only releases it after 5 seconds but here both the threads executed immediately and then "EOM" got printed after 5 seconds.