LogiQuest
Back to Threads
9

What is the output of the following program?

public class Main {
    static int count = 0;
    static synchronized void inc() { count++; }
    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(() -> { for (int i = 0; i < 1000; i++) inc(); });
        Thread b = new Thread(() -> { for (int i = 0; i < 1000; i++) inc(); });
        a.start(); b.start();
        a.join(); b.join();
        System.out.println(count);
    }
}
Hard

Discussion (0)

  • No discussion yet. Ask a doubt or share a shortcut.

Log in to join the discussion.