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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:44:55+00:00 2026-06-11T18:44:55+00:00

I think I have a fairly firm grasp on using the synchronized keyword to

  • 0

I think I have a fairly firm grasp on using the synchronized keyword to prevent inconsistencies between threads in java, but I don’t fully understand what happens if you don’t use that keyword.

Say for instance that I have a field accessed/modified by two threads:

private String sharedString = "";

class OneThread extends Thread {

  private Boolean mRunning = false;

  public OneThread() {}

  public synchronized setRunning(Boolean b) {
    mRunning = b;
  }

  @Override
  public void run() {
    while (mRunning) {
      // read or write to shared string
      sharedString = "text from thread 1";
      System.out.println("String seen from thread 1: " + sharedString);
      super.run();
    }
  }
}

class AnotherThread extends Thread {

  private Boolean mRunning = false;

  public AnotherThread() {}

  public synchronized setRunning(Boolean b) {
    mRunning = b;
  }

  @Override
  public void run() {
    while (mRunning) {
      // read or write to shared string
      sharedString = "text from thread 2";
      System.out.println("String seen from thread 2: " + sharedString);
      super.run();
    }
  }
}

Since both of these threads are accessing and modifying the field sharedString without using the synchronized keyword, I would expect inconsistencies. What I am wondering is what actually happens though. While debugging, I have stepped carefully through both threads in situations like this and noticed that even while one thread is paused, it’s state can be “sticky”.

For the sake of the above example, suppose both threads are paused in the debugger. If I step through one of the threads and leave the other paused, I would expect it would operate like a single threaded application. Yet, many times right after modifying the field, the next line that accesses it retrieves the “wrong” value (a value inconsistent with what it was just modified to).

I know that this code is not good.. but I am asking the question because I’m hoping someone could provide an answer that gives some insight into what actually happens in the virtual machine when multi-threaded applications are implemented poorly. Does the thread who’s field modification attempt was unsuccessful have any effect at all?

If after poorly implementing multi-threaded code we are simply in the realm of “undefined” behavior, and there is no value in learning about this behavior, I’m ok with that.. just a multi-threading noob trying to understand what I observe in the debugger.

  • 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-11T18:44:57+00:00Added an answer on June 11, 2026 at 6:44 pm

    This is due to another critical function of synchronization across threads in Java: preventing data staleness. As part of the Java Memory Model, a Java thread may cache values for shared data. There is no guarantee that a thread will ever see updates made by another thread unless either the shared mutable data is accessed in synchronized blocks or it is marked as volatile. See here for more information.

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

Sidebar

Related Questions

I have what I think should be a fairly simple mapping issue, but not
I have a fairly simple problem (I think) that involves updating a number using
I am a bit new to R, but I think I have a fairly
I have fairly simple problem but I can not think of the simple solution.
I think I have a basic understanding of this, but am hoping that someone
I have an issue that (I think) might have to do with scope, but
I think I have a problem in understanding the proper way of using MVC.
I think I have a basic understanding of REST, but something I'm stuck on
I have some fairly complex java programs with lots of if/else statements where various
I asked a fairly poor question yesterday, but I still have the same general

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.