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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:56:55+00:00 2026-06-16T17:56:55+00:00

According to the Java Language Specification ( Example 17.4-1 ) the following snippet (starting

  • 0

According to the Java Language Specification (Example 17.4-1) the following snippet (starting in A == B == 0)…

Thread 1             Thread 2
--------             --------
r2 = A;              r1 = B;
B = 1;               A = 2;

… can result in r2 == 2 and r1 == 1. This is because the result of executing B = 1; does not depend on whether or not r2 = A has been executed, thus the JVM is free to swap the order of the execution of those two instructions. In other words, the following interleaving is allowed by the spec:

Thread 1             Thread 2
--------             --------
B = 1;
                     r1 = B;
                     A = 2;
r2 = A;

which clearly results in r2 == 1 and r1 == 1.

My question:

Suppose we tweak the example a little:

Thread 1             Thread 2
--------             --------
r2 = A;              r1 = B;
monitorenter obj     monitorenter obj
monitorexit obj      monitorexit obj
B = 1;               A = 2;

where obj is a reference shared between the threads.

Is the reordering of r2 = A and B = 1 still allowed?


The JLS says…

However, compilers are allowed to reorder the instructions in either thread, when this does not affect the execution of that thread in isolation.

…which sort of indicates that the instructions may still be swapped. On the other hand, the following statement

An unlock on a monitor happens-before every subsequent lock on that monitor.

indicates that under certain schedulings we may have a happens-before relation between the statements in the two threads, which presumably disallows instruction reordering.

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

    Informally, it is not allowed. This is known as the “roach motel model”

    http://jeremymanson.blogspot.com/2007/05/roach-motels-and-java-memory-model.html

    Particularly, an action cannot be moved across a synchronization block.

    However, formally, JMM does not speak in terms of reordering. In your example, we can only reason that,

    1. either the sync block 1 is before the sync block 2 in the total synchronization order, therefore r2=A happens-before A=2;r2 must be 0. But there are no constraints between B=1 and r1=B; r1 can be 0 or 1.

    2. or the other way around. r1 must be 0, r2 can be 0 or 2.

    So the program still contains data race; nontheless, we can reason that (r1,r2) can only be (0,0), (1,0), (0,2); it is impossible to be (1,2)

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

Sidebar

Related Questions

According to the Java Language Specification , constructors cannot be marked synchronized because other
According to Java thread state info calling wait() will result a thread to go
According to the JLS (Java Language Specification): The notion of subsignature is designed to
According to the Java Language Specification : If there are any enclosing try statements
In Java we use System.setProperty() method to set some system properties. According to this
According to the Java Language Sepecification , 3rd edition: It is a compile-time error
According to Goetz in his book JCIP : Because each thread has its own
I saw this Java snippet in the book Spring in Action , but I'm
According to the documentation of java.util.Pattern , the POSIX character class \p{Graph} ( [:graph:]
According to the documentation for getResultSet in java.sql.Statement , it says: Retrieves the current

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.