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

  • Home
  • SEARCH
  • 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 8308117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:42:10+00:00 2026-06-08T18:42:10+00:00

I got some questions about AtomicReference.compareAndSet() method, according to the doc, it said: Atomically

  • 0

I got some questions about AtomicReference.compareAndSet() method, according to the doc, it said:

Atomically sets the value to the given updated value if the current value == the expected value.

As far as I understand, the == operator is comparing the address of two objects, if so how will it work in examples like this

private AtomicReference<AccessStatistics> stats =
    new AtomicReference<AccessStatistics>(new AccessStatistics(0, 0));
public void incrementPageCount(boolean wasError) {
    AccessStatistics prev, newValue;
    do {
        prev = stats.get();
        int noPages = prev.getNoPages() + 1;
        int noErrors = prev.getNoErrors;
        if (wasError) {
           noErrors++;
        }
        newValue = new AccessStatistics(noPages, noErrors);
    } while (!stats.compareAndSet(prev, newValue));
}

In this code snippet, how does jvm know which fields of AccessStatistics are to be compared in the compareAndSet()? In fact I’m just wondering how is this whole strategy working given java doesn’t allow to override == at all?
Thanks for any comments!

  • 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-08T18:42:12+00:00Added an answer on June 8, 2026 at 6:42 pm

    how does jvm know which fields of AccessStatistics are to be compared in the compareAndSet()?

    It doesn’t. It is not comparing the fields in the object. It is just comparing the reference of the object which is what the documentation says. That’s just how the AtomicReference class works. As you mention from the javadocs, it uses == and not the equals() method.

    Atomically sets the value to the given updated value if the current value == the expected value.

    All of the Atomic* classes have similar functions. It allows you to atomically set values while being sure that another thread doesn’t overwrite your value. With compareAndSet(...) you have to specify the current object reference to ensure you are updating as expected.

    In your code snippet, it is trying to add to an immutable access-statistics object. So it gets the current value, adds to it, and then stores the new statistic to the reference. If another thread stored its stats between that time, then the compareAndSet will return false and it loops around and tries again. This solves race conditions without having to have a synchronized block.

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

Sidebar

Related Questions

I've got some questions about two ways to save settings in the web.config. Appsettings
I've got some questions about Java's assigment. Strings I've got a class: public class
Reading some questions here on SO about conversion operators and constructors got me thinking
I've got some basic questions about C++. Consider the following code in which I
Got some questions about the php date function.... Here it goes.. I use jquery's
I got some questions about ListViews. first question : I got 5 listviews made
I have got some questions about Dotnetnuke: Can i create/run multiple websites having only
I'm just getting into Facebook application development. I've got some questions about how the
Good morning. As the title indicates, I've got some questions about using python for
I have got some questions about autorelease feature. Please advice.. Q1) Does it have

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.