#105 Race Condition When Reading and Writing Static Fields

Companies:
Sberbank
Themes:
JavaConcurrencyCode Review

What will be printed to the screen?

public class Main extends Thread {
    private static int a;
    private static int b;

    @Setter
    private boolean setterOrGetter;

    @Override
    public void run() {
        if (setterOrGetter) {
            a = 2;
            b = 4;
        } else {
            System.out.println("a = " + a + " b = " + b);
        }
    }

    public static void main(String ... args) {
        Main thread1 = new Main();
        Main thread2 = new Main();
        thread1.setSetterOrGetter(true);
        thread1.start();
        thread2.start();
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in