Analyze the following code snippet. Which statement correctly describes its behavior?

Java interview question for Advanced practice.

Answer

There is no concurrency bug; the code is correct because the lambda is stateless.

Explanation

The code is functionally correct and thread-safe, though intentionally slow. The lambda expression used in the map operation is stateless; the calculation n n depends only on the input n and does not modify any shared state. Thread.sleep() merely pauses the current thread and doesn't introduce a race condition. The collect(Collectors.toList()) operation is also thread-safe for parallel streams. Therefore, there is no concurrency bug.

Related Questions