A useful feature of AtomicBoolean is the ability to continue one thread if and only if BUSY is false, but does not allow other threads to continue working.
private static AtomicBoolean BUSY = new AtomicBoolean(false); public void doWork(){ if(BUSY.compareAndSet(false,true)){
So, here only one thread will be doWork at any given time. You cannot achieve this with volatile boolean , because you cannot be sure that Thread.currentThread() set a boolean.
source share