this is a race. You need to put the compareAndSet in the if block. It could have been set after you called .get.