Synchronization In Java Part 4

BIS
2 min readJan 25, 2020

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.

Online Test Software

Exam Software

Examination Software

Question Bank Software

Online Exam Software

Online Test System

Online Examination Software

Online Assessment System

Assessment Management Software

Multiple Choice Question Software

Examination Management Software

Exam Management Software

Examination Management System

Exam Management System

Online Examination System

MCQ Software

Online Assessment Software

Test Management Software

Online Assessment Software

Exam System

Examination System

RHEED Software

MLM Software

Service Management Software

Online Timesheet Software

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

BIS
BIS

Written by BIS

Specialist in Cloud Native development

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations