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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:59:51+00:00 2026-06-05T17:59:51+00:00

I used to believe that any variable that is shared between two threads, can

  • 0

I used to believe that any variable that is shared between two threads, can be cached thread-locally and should be declared as volatile. But that belief has been challenged recently by a teammate. We are trying to figure out whether volatile is required in the following case or not.

class Class1
{
   void Method1()
   {
      Worker worker = new Worker();
      worker.start();
      ...
      System.out.println(worker.value); // want to poll value at this instant
      ...
   }

   class Worker extends Thread
   {
      int value = 0; // Should this be declared as a volatile?
      public void run()
      {
         ...
         value = 1; // this is the only piece of code that updates value
         ...
       }
   }
}

Now my contention is that, it is possible that the Worker (child) thread could have cached the variable “value” of the Worker object within the thread and updated just it’s copy while setting the value to 1. In such a case, main thread may not see the updated value.

But my teammate believes that since the access to “value” is happening through an object (worker), therefore for both the threads to see different values, it could only be possible if both the threads were maintaining separate copies of “worker” object itself (which would further mean that creation of a thread involves creating a deep copy of all the shared objects).

Now I know that that can’t be true, for it would be highly inefficient for each thread to maintain wholly different copies of all shared objects. So hence, I am in serious doubt. Does doing a “worker.value” in the main thread reference a different memory location than doing a “this.value” in the child thread? Will the child (Worker) thread cache “value”?

Regards.

  • 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-05T17:59:53+00:00Added an answer on June 5, 2026 at 5:59 pm

    Now my contention is that, it is possible that the Worker (child) thread could have cached the variable “value” of the Worker object thread-locally and updated just it’s copy while setting the value to 1. In such a case, main thread may not see the updated value.

    You are correct. Even though you are both dealing with the same Worker instance, there is no guarantee that the cached memory version of the Worker‘s fields have been synchronized between the various different thread memory caches.

    The value field must be marked as volatile to guarantee that other threads will see the value = 1; update to the value field.

    But my teammate believes that since the access to “value” is happening through an object (worker), therefore for both the threads to see different values, it could only be possible if both the threads were maintaining separate copies of “worker” object itself…

    No, this is not correct. The tricky part about thread memory revolves around processor memory caches. Without a memory barrier that is imposed by volatile, a process is completely free to cache memory. So even though both threads would be working with the same instance of the Worker, they may have a locally cached copy of the memory associated with Worker.

    Thread architectures get much of their speed because they are working with separate high-speed processor-local memory as opposed to always referencing central storage.

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

Sidebar

Related Questions

Are there any known methods that can be used to make your software immune
I believe that the Hamiltonian cycle problem can be summed up as the following:
Not used set_intersection before, but I believe it will work with maps. I wrote
I used to export blender models to use them in with three.js. That was
I work on C/C++ using Visual Studio 2008. I believe that I am not
I'm using Saxon-EE 9.3.0.4 for xsl transformations and found that when <xsl:result-document> is used
I'm aware of the fact that the containers from standard library are not thread-safe.
A coding style presentation that I attended lately in office advocated that variables should
Let me start with some background: By tribool I understand a variable which can
I am writing a JavaScript function that can perform decimal place manipulation on a

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.