I'm trying to use an AtomicInteger variable as lock. So, wondering if the code posted below is thread safe. I know incrementAndGet() is an atomic operation. But, I'm not sure if the subsequent '==' operation will be atomic as well (what if the value gets incremented to 2, by the time I make the comparison.). So posting this question to hear your thoughts.
private AtomicInteger count = new AtomicInteger(0); //Shared variable
private final int max = 1;
if(count.incrementAndGet() == max) {
//Do something
}