DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Difference Between Volatile and Synchronized Keywords in Java

Difference Between Volatile and Synchronized Keywords in Java

Want to learn more about the difference between volatile and synchronized keywords? Check out this post to learn more about different keywords and variables in Java.

Siddhartha Bhattacharjee user avatar by
Siddhartha Bhattacharjee
·
Aug. 22, 18 · Analysis
Like (23)
Save
Tweet
Share
61.99K Views

Join the DZone community and get the full member experience.

Join For Free

This article covers some basic, but very important, concepts in Java. 

volatile is a field modifier, while synchronized modifies code blocks and methods.

So, we can specify three variations of a simple accessor using those two keywords:

int i1;              
int geti1() {return i1;}

volatile int i2;              
int geti2() {return i2;}        

int i3;
synchronized int geti3() {return i3;}  


Above, we defined three integer variables:  i1 , i2,  and  i3 . And, we defined three corresponding getter methods:  geti1(), geti2(),  and geti3().

geti1() accesses the value currently stored in i1 in the current thread.

Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it's thread, but the value in the current thread could be different from that updated value. In fact, Java demonstrates the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory.

Therefore, it is possible for the "main" memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1, and for thread3 to have a value of 3 for i1 if thread1 and thread2 have both updated i1. But, the updated values have not yet been propagated to the "main" memory or other threads.

On the other hand, geti2() effectively accesses the value of i2 from the "main" memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile must have its data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Generally, volatile variables have a higher access and update overhead than "plain" variables. Typically, threads are allowed to have their own copy of data, which is for better efficiency.

There are two differences between volatile and synchronized:

First, synchronized obtains and releases locks on monitors, which can force only one thread at a time to execute a code block. That's a fairly well-known aspect to synchronized. But synchronized also synchronizes memory. In fact, synchronized synchronizes the whole of the thread memory with "main" memory. So, executing geti3() does the following:

  1. The thread acquires the lock on the monitor for the object this .
  2. The thread memory flushes all its variables, i.e. it has all of its variables effectively read from the "main" memory.
  3. The code block is executed. In this case, this means setting the return value to the current value of i3, which may have just been reset from "main" memory.
  4. Any changes to variables would normally be written out to the "main" memory, but for geti3(), we have no changes.
  5. The thread releases the lock on the monitor for the object this.

So, where volatile only synchronizes the value of one variable between the thread memory and the "main" memory, synchronized synchronizes the value of all variables between the thread memory and the "main" memory and locks and releases a monitor to control the ownership between multiple threads.

From this information, it can be concluded that synchronized is likely to have more overhead than volatile.

Memory (storage engine) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How
  • Memory Debugging: A Deep Level of Insight
  • Building a Scalable Search Architecture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: