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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:12:32+00:00 2026-05-14T03:12:32+00:00

The java meomry model mandates that synchronize blocks that synchronize on the same monitor

  • 0

The java meomry model mandates that synchronize blocks that synchronize on the same monitor enforce a before-after-realtion on the variables modified within those blocks. Example:

// in thread A
synchronized( lock )
{
  x = true;
}

// in thread B
synchronized( lock )
{
  System.out.println( x );
}

In this case it is garanteed that thread B will see x==true as long as thread A already passed that synchronized-block. Now I am in the process to rewrite lots of code to use the more flexible (and said to be faster) locks in java.util.concurrent, especially the ReentrantReadWriteLock. So the example looks like this:

EDIT: The example was broken, because I incorrectly transformed the code, as noted by matt b. Fixed as follows:

// in thread A
lock.writeLock().lock();
{
  x = true;
}
lock.writeLock().unlock();

// in thread B
lock.readLock().lock();
{
  System.out.println( x );
}
lock.readLock().unlock();

However, I have not seen any hints within the memory model specification that such locks also imply the nessessary ordering. Looking into the implementation it seems to rely on the access to volatile variables inside AbstractQueuedSynchronizer (for the sun implementation at least). However this is not part of any specification and moreover access to non-volatile variables is not really condsidered covered by the memory barrier given by these variables, is it?

So, here are my questions:

  • Is it safe to assume the same ordering as with the “old” synchronized blocks?
  • Is this documented somewhere?
  • Is accessing any volatile variable a memory barrier for any other variable?

Regards,
Steffen

—

Comment to Yanamon:

Look at the following code:

// in thread a
x = 1;
synchronized ( a ) { y = 2; }
z = 3;

// in thread b
System.out.println( x );
synchronized ( a ) { System.out.println( y ); }
System.out.println( z );

From what I understood, the memory barrier enforces the second output to show 2, but has no guaranteed affect on the other variables…? So how can this be compared to accessing a volatile variable?

  • 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-14T03:12:33+00:00Added an answer on May 14, 2026 at 3:12 am

    From the API-doc:

    All Lock implementations must enforce
    the same memory synchronization
    semantics as provided by the built-in
    monitor lock, as described in The
    Java Language Specification, Third
    Edition (17.4 Memory Model):

    * A successful lock operation has the same memory synchronization effects as a successful Lock action.
    * A successful unlock operation has the same memory synchronization effects as a successful Unlock action.
    

    Unsuccessful locking and unlocking
    operations, and reentrant
    locking/unlocking operations, do not
    require any memory synchronization
    effects.

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

Sidebar

Related Questions

When using any of the java.util.concurrent classes, do I still need to synchronize access
I'm interested in how java organizes memory and executes code (like what gets put
Question I'm looking for a Java in-memory object caching API. Any recommendations? What solutions
Does Java 6 consume more memory than you expect for largish applications? I have
How do you find a memory leak in Java (using, for example, JHat)? I
I am using jProfiler to find memory leaks in a Java swing application. I
I've managed to get a memory 'leak' in a java application I'm developing. When
The default 64 MB max heap memory can be small for a large Java
When an object is instantiated in Java, what is really going into memory? Are
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism in Java.

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.