Menu
Question Index
...

What is the output of the following program?

import java.util.concurrent.atomic.*;

public class Modi {
    public static void main(String[] args) {
        new Rope("M");  new Rope("E");
        new Rope("R");  new Rope("I");
        new Rope("T");
    }
}
class Rope implements Runnable {
    String name;
    public Rope(String name) {
        this.name = name;
        new Thread(this).run();
    }
    public void run() {
        for (int i = 1; i < 2; i++) {
            System.out.print(name + " : " + Shared.ai.getAndSet(i) + " ");
        }
    }
}
class Shared {
    static AtomicInteger ai = new AtomicInteger(2);
}


M : 2 E : 1 R : 0 I : 0 T : 0
M : 2 E : 1 R : 1 I : 1 T : 1
M : 2 E : 2 R : 2 I : 2 T : 2
M : 1 E : 1 R : 1 I : 1 T : 1
Compilation or Runtime Error

Doubts

Problems

Topic: Other Advanced Topics In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App