Menu
Question Index
...

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 - B - M - Y - C - D -
A - B - X - C - X - D - X - E -
A - B - M - C - D - N - E -
Some other output not shown here
Compilation or Runtime Error

Doubts

Problems

Topic: What is Perl?

Read this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App