Menu
Question Index
...

What will be the output of the following program?

public class NewQuestion {
    public static void main(String[] args) {
        new VolatileTest("Merit").run();
        new VolatileTest("Campus").run();
    }
}
class VolatileTest extends Thread {
    private volatile int testValue;
    private volatile boolean ready;
    public VolatileTest(String str) {
        super(str);
    }
    public void run() {
        for (int i = 0; i < 2; i++) {
            try {
                if (getName().equals("Merit")) {
                    ready = true;
                    testValue = i;
                    System.out.println(getName() + ", " + ready + ", " + testValue);
                }
                if (getName().equals("Campus")) {
                    System.out.println(getName() + ", " + ready + ", " + testValue);
                }
                Thread.sleep(1000);
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            }
        }
    }
}


Merit, true, 0
Campus, false, 0
Merit, true, 1
Campus, false, 0
Merit, true, 0
Merit, true, 1
Campus, false, 0
Campus, false, 0
Merit, true, 0
Campus, false, 0
Campus, false, 0
Merit, true, 1
Some other output
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: What is Multitasking and Multithreading?

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App