Interview Questions

Output question 4.

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

64. Output question 4.

class MyRunnable implements Runnable{
public void run(){
synchronized (this) {
System.out.println("1 ");
try {
this.wait();
System.out.println("2 ");
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}
}

public class MyClass {

public static void main(String[] args) {

MyRunnable myRunnable=new MyRunnable();
Thread thread1=new Thread(myRunnable,"Thread-1");
thread1.start();

}
}



Answer.
Thread acquires lock on myRunnable object so 1 was printed but notify wasn't called so 2 will never be printed, this is called frozen process. Deadlock is formed, These type of deadlocks are called Frozen processes.

/*OUTPUT
1
*/

(Continued on next question...)

Other Interview Questions