#436 Problems with parallelStream and a Non-Thread-Safe Collection

Companies:
Alfa-Bank
Themes:
JavaConcurrencyCode Review

What will be printed, and what problems may occur?

public final class Main {

    public static void main(String[] args) {
        Set<String> threads = new HashSet<>();

        var integers = IntStream.range(0, 7000).boxed().collect(Collectors.toList());
        integers.stream().parallel().forEach(v -> threads.add(Thread.currentThread().getName());

        System.out.println(threads.size());

        //System.out.println(threads);
    }
}

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