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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:31:50+00:00 2026-06-03T06:31:50+00:00

I’m using java for about a month, and still am generally an amateur in

  • 0

I’m using java for about a month, and still am generally an amateur in programming, so feel free to correct me if I get something wrong. Maybe I’ll provide some excess details, but I’m so confused right now that I can’t decide what matters anymore.

So, I’ve been developing multi-threaded client-server application. All threads are using the same object, in which certain configuration values and shared loggers are stored; this object is initialized in server-thread and then passed as an argument to client-thread class constructor. First it was assumed that fields of that object are changed only once, when server starts, so concurrent access was nothing to worry about, but now it’s required for some configuration values to be re-read from config file when it’s modified, without having to restart the server.

The first idea that came to mind after some research was to make a synchronized method that would be called when some values from the class are requested, and would re-read those values if our config file changed since last access and return immediately otherwise, like this:

<This code is inside "config" class, instance of which is shared between threads>
private static long lastModified;
private static File configFile;

public class ChangingVariableSet
    {
    <changing variables go here>
    }

private synchronized void ReReadConfig
    {
    long tempLastMod = configFile.lastModified();
    if(lastModified == tempLastMod)
        return;
    <reread values here>
    lastModified = tempLastMod;
    }

public ChangingVariableSet GetValues()
    {
    ReReadConfig();
    <return necessary values>
    }

(The above code isn’t tested, i just want to get the general idea across).

But I just didn’t like the idea of blocking every single time the value is requested, since that seems expensive, and my application has a possibility of becoming pretty high-loaded with lots of threads in the future. So I had a “good” idea – to check if file is modified before locking and then inside the locked method again, to avoid locking at all whenever possible:

 public ChangingVariableSet GetValues()
    {
    if(lastModified == configFile.lastModified())
        ReReadConfig();
    <return necessary values>
    }

Ten minutes later I learned it’s called double-checked locking and another ten minutes later my world crumbled twice after reading this article: first time when I learned it supposedly would not work due to internal CPU caching and second time when I read about operations on long/float types not being atomic. Or will it work after all, since no object creation is involved? And, since operations on long are non-atomic, would it really be enough to declare “lastModified” as volatile? If possible, I would prefer a proper explanation on why it will/will not work. Thank you in advance.

P.S: I know similar questions were already answered couple of times, and maybe it would be better to stop nitpicking and synchronize the whole “getValue” method rather than “ReReadConfig”, but I’m striving to learn more about thread-safe programming and to see pitfalls in my own code to avoid something similar in the future. I also apologize for any possible grammar and spelling errors, I don’t know English that well.


EDIT: First, I fixed a typo in last “if” clause. Second – warning, the above code is NOT thread-safe, do not use it! In method

 public ChangingVariableSet GetValues()
    {
    if(lastModified == configFile.lastModified())
        ReReadConfig();
    <return necessary values>
    }

In case file was updated in time span between if-check and value returning, thread B could start ReReadConfig BEFORE thread A starts returning values, resulting in a dangerous partial changes to necessary data. It seems the correct way of doing what I need without excessive blocking is using ReentrantReadWriteLock, however, I still want to use double-checking to avoid excessive (and expensive, file is assumed to be large XML) config re-reads:

<...>
private static final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private static final Lock read  = readWriteLock.readLock();
private static final Lock write = readWriteLock.writeLock();

private void ReReadConfig
    {
    write.lock();
    long tempLastMod = configFile.lastModified();
    if(lastModified == tempLastMod)
        return;
    <reread values here>
    lastModified = tempLastMod;
    write.release();
    }

 public ChangingVariableSet GetValues()
    {
    if(lastModified == configFile.lastModified())
        ReReadConfig();
    read.lock();
    <get necessary values>
    read.release();
    <return necessary values>
    }

Now it at least looks thread-safe, but question remains open on depending on volatile “lastModified” variable when checking: I’ve read somewhere that volatile variables can’t guarantee anything on non-atomic operations, and “long” type read/writes ARE non-atomic.

  • 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-03T06:31:52+00:00Added an answer on June 3, 2026 at 6:31 am

    You want to use a ReadWriteLock. This does not block readers as long as there are no writers.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.