问题
I write a thread class called T.
My purpose is to make sure only one thread object running at a time.
So when the thread object is called, it would check a boolean flag called BUSY.
My question is what is the different between
private static AtomicBoolean BUSY = new AtomicBoolean(false);
and
private static boolean BUSY = false;
I thought if using the 'static', all object would only check one BUSY boolean variable so that would make sure only one thread object is running.
回答1:
You must at least make the boolean variable volatile
and the AtomicBoolean variable final
in order to have a comparable solution. After you do that, there will be no difference for your use case.
The difference comes about if you use AtomicBoolean
's getAndSet
or compareAndSet
methods, which combine one read and one write action into an atomic whole, wheraas these are not atomic when done against a volatile
.
回答2:
You can use a boolean
and with proper synchronization (and making volatile
) can achieve what you need.
But by using the AtomicBoolean
you can check the current value atomically without the need to write code for synchronization yourself
回答3:
As said by others you must make the variables final / volatile respectively.
From what you describe I fear you have a place where you do something like this:
if (!BUSY){
BUSY = true;
}
Note that this is broken in absence of commons synchronization, because two threads might check the flag, both seeing it as false and starting their work.
I suggest looking into the existing structures for handling concurrency: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-summary.html
Especially Semaphore with a single permit might be what you are looking for.
回答4:
A useful feature of an AtomicBoolean
is to allow a single thread to continue if and only if BUSY is false but not to let any other threads to continue.
private static AtomicBoolean BUSY = new AtomicBoolean(false);
public void doWork(){
if(BUSY.compareAndSet(false,true)){
//do some work
BUSY.set(false);
}else{
//some other thread won, return now or retry later?
}
}
So here only one thread will doWork
at any given time. You cannot achieve this with a volatile boolean
because you cannot be sure that Thread.currentThread()
set the boolean.
来源:https://stackoverflow.com/questions/12035176/java-what-is-the-different-between-atomicboolean-and-a-static-boolean-variable