Output question 6.
Java Interview Questions and Answers (part 4)
(Continued from previous question...)
66. Output question 6.
class MyRunnable implements Runnable{
public void run(){
synchronized (this) {
System.out.print("1 ");
try {
this.wait(1000);
System.out.print("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.
First 1 will be printed then even if notify() or notifyAll() is not called, thread will be notified after 1000 millisec and 2 will be printed.
/*OUTPUT
1 2
*/
(Continued on next question...)
Other Interview Questions
|