Java provides concurrent package and makes it easier for the developer to deal with multi-threading applications.
Atomic Access
Writes and reads of volatile long and double are always atomic. Write to reads of references are always atomic.
For int, char, byte, short, boolean, float, reads/writes are ALWAYS atomic
For double and long, if they’re volatile, reads/writes are ALWAYS atomic
Java provides a concurrent package to deal with atomicity elegantly.
AtomicInteger
AtomicBoolean
AtomicLong
AtomicReference
AtomicStampedReference
AtomicIntegerArray
AtomicLongArray
AtomicReferenceArray
The main advantage is that for instance atomic counter incrementAndGet that can be used by many threads concurrently. This is achieved by supporting compare-and-swap instruction (compareAndSet()) to code non-blocking algorithms.
CAS (Compare and Set ) counters using atomic variable have better performance than lock-based counters. The primary use of AtomicInteger is where index++ and index — are not possible with the volatile variable ad wants to avoid using synchronize marker. The assignment and retrieval are atomic but has many simple operations getAndXXX or xXXAndGet like getAndIncrement, compareAndSet etc.
The AtomicInteger class uses CAS (Compare and Swap ) low-level CPU operations (NO SYNCHRONIZATION IS NEEDED).
There are a number of issues with the synchronized keyword which we will discuss in the later chapters. Business Integration Software has a number of training modules regarding synchronization.
Assessment Management Software
Multiple Choice Question Software