#89 Increment and Decrement Problems in Multithreading

Companies:
X5
Themes:
JavaConcurrencyCode Review

What will be printed to the screen? What problems are present? How can they be fixed?

public class Main {
    public static void main(String[] args) {
        final int[] i = {0};

        for (int j = 0; j < 10; j++) {
            for (int c = 0; c < 10; c++) {
                new Thread(() -> {
                    ++i[0];
                    Thread.sleep(3L);
                    --i[0];
                }).start();
            }
        }

        System.out.println(i[0]);
    }
}

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