Menu
Topics Index
...
`

Wait and Notify


If you need explanation Read this topic

Wait and Notify
What will be the output of the following program?
public class WaitAndNotify extends Thread {

    public static void main(String[] args) {
        WaitAndNotify wan = new WaitAndNotify();
        print("A"); wan.start();
        print("B"); wan.doWait(20000);
        print("C"); wan.doWait(1);
        print("D"); wan.doWait(50000);
        print("E");
    }

    public void doWait(long l) {
        synchronized (this) {
            try { wait(l); } catch (Exception e) { print("X"); }
        }
    }

    public void doNotify() { synchronized (this) { notify(); } }
    public void doNotifyAll() { synchronized (this) { notifyAll(); } }
    public static void print(String s) { System.out.print(s + " - "); }

    public void run() {
        try {
            Thread.sleep(5000);
            print("M"); doNotify();
            Thread.sleep(2000);
            print("N"); doNotifyAll();
        } catch (Exception e) { print("Y"); }
    }
}

A. A - B - M - Y - C - D - 
B. A - B - X - C - X - D - X - E - 
C. A - B - M - C - D - N - E - 
D. Some other output not shown here
E. Compilation or Runtime Error
Topic: What is Perl?

If you need explanation Read this topic

User comments below. All of them might not be correct.

Ans is c

Posted by Sai Ram    2015-03-19 06:41:05


Ans is c

Posted by Mânïshå Mùlchåndânï    2015-03-19 09:26:43


This dose is now closed and the winners are Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-20 06:54:41


© meritcampus 2019

All Rights Reserved.

Open In App