Output question 15.
Java Interview Questions and Answers (part 4)
(Continued from previous question...)
75. Output question 15.
class MyRunnable implements Runnable{
public void run(){
System.out.println("1 ");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("2 ");
}
}
public class WaitNoParaMethod {
public static void main(String[] args) {
MyRunnable myRunnable=new MyRunnable();
Thread thread1=new Thread(myRunnable,"Thread-1");
thread1.start();
}
}
Answer.
IllegalMonitorStateException is thrown at runtime, as wait() method was called without acquiring lock on object monitor.
/*OUTPUT
1
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at o15_wait_IllegalMoni.MyRunnable.run(WaitNoParaMethod.java:9)
at java.lang.Thread.run(Unknown Source)
*/
(Continued on next question...)
Other Interview Questions
|