Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7492941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:40:19+00:00 2026-05-29T16:40:19+00:00

I have an application which are multi threading. I notice some existing code use

  • 0

I have an application which are multi threading. I notice some existing code use volatile when the variable is shared by several threads. Why not just use synchronized in the method when variable is used, what’s the benefits to define variable as volatile?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:40:21+00:00Added an answer on May 29, 2026 at 4:40 pm

    Declaring a volatile Java variable means:

    • The value of this variable will never be cached thread-locally: all reads and writes will go straight to “main memory”;
    • Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.

    In other words, the main differences between synchronized and volatile are:

    • a primitive variable may be declared volatile (whereas you can’t synchronize on a primitive with synchronized);
    • an access to a volatile variable never has the potential to block: we’re only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock;
    • because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we’re prepared to “miss an update”);
    • a volatile variable that is an object reference may be null (because you’re effectively synchronizing on the reference, not the actual object).

    more information is: http://javamex.com/tutorials/synchronization_volatile.shtml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some trouble with threading in my application. I have a multi-threaded client/server
I have a WPF application which uses some library code for authentication which needs
I have an MFC application to which I want to incorporate multi-threading. Originally I
I have a layered application in Java which has a multi thread data access
I have a multi-threaded application in C# which tries to write to a TextBox
I have application which needs to use a dll (also written by me) which
We have an application which needs to use Direct3D. Specifically, it needs at least
We have an application which is multi-tenant (lots of users all accessing their own
I have a multi-threaded application which scales well to begin with, but running on
I have written code to save an image which is generated by the application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.