I have the following method which is being called by multiple threads:
private final static Object lock = new Object();
public String createDirectory()
{
File file = new File("D:"+File.separator+"test");
if(!file.exists() || !file.isDirectory())//if file doesn't exist then create a new directory.
{
synchronized(lock)
{
if(!file.exists() || !file.isDirectory())//----> (1)
{
boolean isCreated = file.mkdir();
}
}
}
return file.getAbsolutePath();
}
Is it possible that the JVM optimizer will comment out the code marked as (1) in above given menthod? I am suspecting that because, the existence of directory is checked twice in immediate succession. Considering it as a unnecessary redundant checking JVM optimizer might comment out the line --> (1).