I'm using AtomicInteger
to compare and set synchronization state. Here is it
private final AtomicInteger state = new AtomicInteger(1);
public void tryDo(){
if(state.compareAndSet(1, 2)){
//do some usefule
}
}
The question is if the following scenario possible:
state = 1
- Two or more threads trying to
compareAndSet
the state to2
- All the threads at
2.
fails and the state remains1
Is it documented? Is it platform dependent? How about x86-64
?