Atomic Classes useful in Multi Threaded environment
Use AtomicInteger when:
your environment is multi threaded
you need to update a counter of type Integer
there are not many instances of the class containing the counter. If there are many instances you should use AtomicIntegerFieldUpdater instead as it has lower memory requirements.
For example see: AtomicInteger
Use AtomicLong when:
your environment is multi threaded
you need to update a counter of type Long
there are not many instances of the class containing the counter. If there are many instances you should use AtomicLongFieldUpdater instead as it has lower memory requirements
Use AtomicBoolean when:
your environment is multi threaded
you need to update a Boolean atomically
Use AtomicReference when:
your environment is multi threaded
you need to update a reference to an object atomically
you want to update more than value at once. In this case create an immutable object that contains multiple values and update it using AtomicReference
there are not many instances of the class containing the reference. If there are many instances you should use AtomicReferenceFieldUpdater instead as it has lower memory requirements
For example see: AtomicReference
Use AtomicMarkableReference when:
your environment is multi threaded
you need to save a reference to an object and a boolean at the same time atomically. (Internally AtomicMarkableReference creates a mutable object containing the reference and boolean and then does the equivalent of AtomicReference).
Use AtomicStampedReference when:
your environment is multi threaded
you need to save a reference to an object and an Integer at the same time atomically. (Internally AtomicMarkableReference creates a mutable object containing the reference and boolean and then does the equivalent of AtomicReference).
you need to prevent ABA occuring (A changing to B and then back to A again)
For example see: AtomicStampedReference
Use AtomicLongFieldUpdater when:
your environment is multi threaded
you have many instances of a class which you want an Long counter in , AtomicIntegerFieldUpdater will save memory over using many AtomicIntegers (if you don't have many instances use AtomicInteger instead)
For example see: AtomicIntegerFieldUpdater
Use AtomicReferenceFieldUpdater when:
your environment is multi threaded
you have many instances of a class which you want a reference to an object within which you wish to be able to update atomically. AtomicReferenceFieldUpdater will save memory over using many AtomicReference (If you don't have many instances use AtomicReference instead)
Use AtomicIntegerArray when:
your environment is multi threaded
you want to use an Integer array that you can update the values atomically with.
Use AtomicLongArray when:
your environment is multi threaded
you want to use a Long array that you can update the values atomically with.
Use AtomicReferenceArray when:
your environment is multi threaded
you want to use an array of object referenceswhich you can update atomically
Use Unsafe when:
your environment is multi threaded
you want fine grained contol over updating arrays or concurrent structures. Note that the other atomic classes and some concurrent structures use Unsafe internally. You will need to use reflection to grant access so that you can call the " theUnsafe" field and get an instance to Unsafe.
you wish to allocate memory yourself and get an address for the memory which you can use to update the memory address yourself directly with.
For example see: Unsafe
Back: Data Structures
Page Author: JD