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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:22:21+00:00 2026-05-27T02:22:21+00:00

This maybe a related question: Java assignment issues – Is this atomic? I have

  • 0

This maybe a related question: Java assignment issues – Is this atomic?

I have the same class as the OP that acts on a mutable string reference. But set rarely happens. (basically this string is part of a server configuration that only reloads when forced to).

public class Test {
 private String s;

 public void setS(String str){
  s = str;
 }

 public String getS(){
  return s;
 }
}

Multiple threads will be pounding this variable to read its value. What is the best method to make it ‘safe’ while not having to incur the performance degradation by declaring it volatile?

I am currently heading into the direction of ReadWriteLock, but as far as I understand, ReadWrite locks does not make it safe from thread caching? unless some syncronisation happen? Which means I’ve gone a full circle back to I may as well just use the volatile keyword?

Is my understanding correct? Is there nothing that can ‘notify’ other threads about an update to a variable in main memory manually such that they can update their local cache just once on a full moon?

volatile on this seems overkill given that the server application is designed to run for months without restart. By that time, it would’ve served a few million reads. I’m thinking I might as well just set the String as static final and not allow it mutate without a complete application and JVM restart.

  • 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-27T02:22:21+00:00Added an answer on May 27, 2026 at 2:22 am

    Reads and writes to references are atomic. The problems you can incur is attempting to perform a read and a write (an update) or guaranteeing that after a write all thread see this change on the next read. However, only you can say what your requirements are.

    When you use volatile, it requires a cache coherent copy be read or written. This doesn’t require a copy be made to/from main memory as the caches communicate amongst themselves, even between sockets. There is a performance impact but it doesn’t mean the caches are not used.

    Even if the access did go all the way to main memory, you could still do millions of accesses per second.

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

Sidebar

Related Questions

This is related to the this question and the answer maybe the same but
I will be tackling a Java (GWT) project soon ( related question ). Maybe
I have a java threads related question. To take a very simple example, lets
not sure if this question should be here or in serverfault, but it's java-related
First this IS a Java question so forgive this first C#-related explanation... I've most
This maybe a stupid question, but as I can not easily undo my change
this maybe a noobish question so sorry, is it possible to run c programmes
I know this maybe a very basic question but I'm having a bit of
I know this maybe a basic question but I just can't seem to find
Ok so this maybe a simple/silly question but I don't know so here goes:

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.