Let's say I have a method that creates a new user for a web application. The method itself calls a static helper class that creates a SQL statement that performs the actual insertion into my DB.
public void createUserInDb(String userName){
SQLHelper.insertUser(userName);
}
I want to synchronize this method such that it cannot be called concurrently by different threads if the passed in parameter (userName
) is the same on those threads. I know I can synchronize method execution using the synchronized keyword, but this would prevent different threads from concurrently executing the method in general. I only want to prevent concurrent execution if the passed in variable is the same. Is there an easy construct in Java that would let me do this?