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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:14:34+00:00 2026-05-15T07:14:34+00:00

I think i have a pretty good idea about the volatile keyword in java,

  • 0

I think i have a pretty good idea about the volatile keyword in java, but i’m thinking about re-factoring some code and i thought it would be a good idea to use it.

i have a class that is basically working as a DB Cache. it holds a bunch of objects that it has read from a database, serves requests for those objects, and then occasionally refreshes the database (based on a timeout). Heres the skeleton

public class Cache
{
    private HashMap mappings =....;
    private long last_update_time;
    private void loadMappingsFromDB()
    {
        //....
    }
    private void checkLoad()
    {
        if(System.currentTimeMillis() - last_update_time > TIMEOUT)
            loadMappingsFromDB();
    }
    public Data get(ID id)
    {
        checkLoad();
        //.. look it up
    }
}

So the concern is that loadMappingsFromDB could be a high latency operation and thats not acceptable, So initially i thought that i could spin up a thread on cache startup and then just have it sleep and then update the cache in the background. But then i would need to synchronize my class (or the map). and then i would just be trading an occasional big pause for making every cache access slower.

Then i thought why not use volatile

i could define the map reference as volatile

private volatile HashMap mappings =....;

and then in get (or anywhere else that uses the mappings variable) i would just make a local copy of the reference:

public Data get(ID id)
{
    HashMap local = mappings;
    //.. look it up using local
}

and then the background thread would just load into a temp table and then swap the references in the class

HashMap tmp;
//load tmp from DB
mappings = tmp;//swap variables forcing write barrier

Does this approach make sense? and is it actually thread-safe?

  • 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-15T07:14:34+00:00Added an answer on May 15, 2026 at 7:14 am

    There’s some misinformation in the existing answers to this question. Use of volatile actually is a good step in ensuring thread safety. See item 3 in Dispelling Java programming language myths by IBM’s Peter Haggar. Haggar gives a bit of background and an example, but the nut is this:

    So, how can atomic operations not be thread safe? The main point is that they may indeed be thread safe, but there is no guarantee that they are. Java threads are allowed to keep private copies of variables separate from main memory.

    By using volatile, you will guarantee that threads are referring to main memory and not using private copies of variables that you don’t know about or expect.

    To answer your question, then: yes, your strategy is safe.

    EDIT:
    In response to another post, here is the JLS section about volatile fields.

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

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm used to doing Java programming, where you never really have to think about
I think this is pretty typical, you have the same website project with an
I think I have a solution to this, but is there a better way,
This follows a couple of other questions (but I think I have refined my
I have a problem which I think is related to forward declarations, but perhaps
I've written an entire app pretty successfully in Django but I have this nagging
I have set up a Django application that uses images. I think I have
Over the years, I think I have seen and tried every conceivable way of
That's the question. Give only one reason you think why have OODB failed or
I think it important to have an undo method ala gmail when destroying records

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.