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 8290921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:57:17+00:00 2026-06-08T12:57:17+00:00

When Synchronization is used there is a performance impact. Can volatile be used in

  • 0

When Synchronization is used there is a performance impact. Can volatile be used in combination with synchronized to reduce the performance overhead ? For example, instance of Counter will be shared among many threads and each thread can access Counter’s public methods. In the below code volatile is used for getter and synchronized is used for setter

public class Counter
{
    private volatile int count;

    public Counter()
    {
        count = 0;
    }

    public int getCount()
    {
        return count;
    }

    public synchronized void increment()
    {
        ++count;
    }   
}

Please let me know in which scenario this might break ?

  • 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-06-08T12:57:20+00:00Added an answer on June 8, 2026 at 12:57 pm

    Yes, you definitely can. In fact, if you look at the source code of AtomicInteger, it’s essentially what they do. AtomicInteger.get simply returns value, which is a volatile int (link). The only real difference from what you’ve done and what they do is that they use a CAS for the increment instead of synchronization. On modern hardware, a CAS can eliminate any mutual exclusion; on older hardware, the JVM will put some sort of mutex around the increment.

    Volatile reads are about as fast as non-volatile ones, so the reads will be quite fast.

    Not only that, but volatile fields are guaranteed not to tear: see JLS 17.7, which specifies that volatile longs and doubles are not subject to word tearing. So your code would work with a long just as well as an int.

    As Diego Frehner points out, you might not see the result of an increment if you get the value “right as” the increment happens — you’ll either see the before or the after. Of course, if get were synchronized you’d have exactly the same behavior from the read thread — you’d either see the before-increment or post-increment value. So it’s really the same either way. In other words, it doesn’t make sense to say that you won’t see the value as it’s happening — unless you meant word tearing, which (a) you won’t get and (b) you would never want.

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

Sidebar

Related Questions

Is the locking object used for synchronization arbitrary or is there reasoning behind choosing
I have a C++ application which used Mutex, Events,Semaphores for synchronization. While hosted in
In the context of data-structures synchronization, can someone clarify the difference between lockless and
I'm wondering what good ways there would be make assertions about synchronization or something
Here's the task: I need to lock based on a filename. There can be
I think in the implementation of ConcurrentHashMap, ReentrantLock has already been used. So there
I am writing data synchronization application between sql server and firebird. In firebird is
The docs say: Contact Synchronization is performed by the Synchronization service. The service loops
Simple question: does the Java memory/synchronization model guarantee atomic pointer writes? That is, if
I have a doubt regarding Java Synchronization . I want to know if I

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.