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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:29:38+00:00 2026-06-15T01:29:38+00:00

What are the semantics of an AtomicReference ? If I do: AtomicReference<CustomObject> ref =

  • 0

What are the semantics of an AtomicReference?
If I do:

AtomicReference<CustomObject> ref = new AtomicReference<CustomObject>();

and then I do:

public void someMethod(){  
 //do something  

 ref.set(loadNewData());  

}  

private final Sempahore updatePermit = new Sempahore(1);  

private CustomObject loadNewData(){  
     CustomObject obj = null;  
     if (updatePermit.tryAcquire()) {  
        obj = ...; //do the loading and create Object  
        updatePermit.release();  
    } else {  
        //update already running, wait  
        updatePermit.acquire();  
        //release the permit immediately  
        updatePermit.release();  
        obj = ref.get(); //????
    }  
    return obj;    
}    

Is there a guarantee that on line obj = ref.get(); //???? the get will return the most fresh version of CustomObject?
This is related to the answer of assylias for post:

  • 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-15T01:29:39+00:00Added an answer on June 15, 2026 at 1:29 am

    Actually, your code has a race condition which could cause new data to be lost:

    1. first caller goes into loadNewData and starts loading new data (has semaphore)
    2. second caller cannot get semaphore and waits at acquire
    3. first caller finishes loading and releases semaphore (but doesn’t return new data yet)
    4. second caller acquires semaphore, calls ref.get() and gets old data
    5. first caller returns new data which is passed to ref.set()
    6. second caller return old data which overwrites new data when passed to ref.set()

    Since someMethod() is always loading new data and you always want callers to wait while new data is loading, all this extra stuff is useless. just use a simple synchronized block (or Lock) around the whole block and ditch the atomic reference. according to the linked post, it seems like you only ever want to do this once, so use an initialized flag.

    private boolean _initialized;
    
    public synchronized void loadLatestData() {
      if(!_initialized) {
          // load latest data ...
          _initialized = true;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A java class does something like the following public class Foo { private final
If I have a class implementing move semantics: class BigObject { public: BigObject(something x
I'd like semantics similar to C# 's ref keyword.
Considering this code with 3 differents function call semantics: void f(void){ puts(OK); } int
Earlier this week, I had to do something which feels like a semantics violation.
The Java language spec defines semantics of final fields in section 17.5 : The
I'm new to move semantics in C++11 and I don't know very well how
I am new to Haskell and to its semantics. I learned that not every
The following code exhibits the semantics that I'd like to replicate on a java
R has pass-by-value semantics, which minimizes accidental side effects (a good thing). However, when

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.