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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:05:55+00:00 2026-05-23T03:05:55+00:00

Say that I update values of two variables in a synchronized method. Would it

  • 0

Say that I update values of two variables in a synchronized method. Would it be possible that the new values set in synchronized method be visible to other threads before exiting the synchronized block?

public synchronized void setValues(){
    a=5;
    // assume thread is preempted after this assignment
    // would the value 5 be visible to other threads?
    // my understanding is that the values will not be flushed to
    // main memory until the lock is released- i.e., until the synchronized
    // method is complete. So the changes will not be visible to other 
    // threads even when not using synchronization
    b=10;
}

Below method is not synchronized, so I understand that the thread may see stale values. My question is if the thread is preempted after assignment to a, is it ever possible that the new value “5” for variable a be visible in printValues method?

public void printValues() {
    System.out.println(a + " " + b);
}
  • 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-23T03:05:56+00:00Added an answer on May 23, 2026 at 3:05 am

    Yes, changes made within synchronized can (but aren’t guaranteed) to be visible before you get to the end of the synchronized block. Basically, you usually need to synchronize (on the same lock) when reading or writing data in order to get a consistent view of the world.

    The guarantees provided for synchronization are to make “doing the right thing” (synchronizing appropriately) work correctly – they don’t guarantee that changes are made atomically when you’re not doing the right thing (observing the shared variables without synchronizing).

    You can (to some extent) think of the writes within the synchronized block as being like calls to OutputStream.write() with the exit of the synchronized block being like a flush() call. When you’re half way through the block, some of the data you’ve written may have made it to the output file (or whatever) – but it might still be buffered. That’s not meant to be an indication of how the memory model is implemented, just an analogy to help you understand how the visibility isn’t guaranteed.

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

Sidebar

Related Questions

Say that I have two models- Users and Accounts. Each account can have at
Say that I have two tables like those: Employers (id, name, .... , deptId).
I have a data object (let's say it's called 'Entry') that has a set
Say that you're developing code which needs to compile and run on multiple hosts
Say that I write an article or document about a certain topic, but the
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Let say that I have a website with some information that could be access
Lets say that I have a header user control in a master page, and
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I want to have a table that logs the date and

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.