Analyze the following concurrent code that uses a shared `BCryptPasswordEncoder`. Which statement is correct?

Java interview question for Advanced practice.

Answer

The code is correct; there is no concurrency issue as BCryptPasswordEncoder is thread-safe.

Explanation

The correct answer is A. This is a trick question. The BCryptPasswordEncoder provided by Spring Security is designed to be thread-safe. Sharing a single instance across multiple threads is the intended and efficient way to use it. Therefore, the code does not have a concurrency problem. Creating a new instance per thread (B) would be inefficient. Using a synchronized block (D) is unnecessary and would hurt performance.

Related Questions