There are Atomic*FieldUpdater classes in java.util.concurrent.atomic package, like:
AtomicIntegerFieldUpdaterAtomicLongFieldUpdaterAtomicReferenceFieldUpdateretc.
But why no AtomicBooleanFieldUpdater?
There are Atomic*FieldUpdater classes in java.util.concurrent.atomic package, like:
AtomicIntegerFieldUpdaterAtomicLongFieldUpdaterAtomicReferenceFieldUpdater etc.But why no AtomicBooleanFieldUpdater?
The only meaningful method that such an updater could possibly have is a basic CAS job. A method that does:
false, set it to true, and return false. If the value is true, then it stays true, and this returns true. And do this all atomically. (It's: set to true, then return the old value, atomically).And that's.. the only method that seems relevant here. Those get() methods in these Updater classes are just for convenience; they don't give you any more atomicity than just thatThing.fieldName; would get you.
There is no point to compareAndSet - the above is identical to it (IF the field is currently false, set it to true, and return true to indicate the CAS job updated. If it is already true, then don't change it, and return false - that is identical to the negation of the above method - walk through all the cases if you're having a hard time seeing how that works).
The only other method this class could have that isn't covered by the above and isn't the just-for-convenience get() is, I guess, lazySet.
It would be useful perhaps, but only slightly so.
One crazy solution would be to just let AtomicIntegerFieldUpdater work for boolean fields as well - they are really just full-word-size in all JVM impls I know of. I checked: The API won't let you do that (throws an error if you attempt to create a new AIFU for a boolean field).