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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:33:59+00:00 2026-05-27T23:33:59+00:00

It is well known that Java Language allows compliers to re-arrange lines of compiled

  • 0

It is well known that Java Language allows compliers to re-arrange lines of compiled code as long as the re-order makes no difference to the code semantics. However , the compiler is required to only bother about sematics as seen from the current thread. If this re-order affects semantics in a multithreaded situation , it usually causes concurrency issues ( memory visibility )

My question(s) :

  1. What is achieved by allowing this freedm to the compiler ? Is it really possible for the compiler to produce code which is more efficient by rearranging the code ? I am yet to see a practical case for this. I feel sometimes that the benefits if any are far outweighed by the concurrency risks this can introduce.

  2. Is there any way that programmer can tell compiler not to rearrange lines like this ? I know that using synchronization primitives effectively handles the side-effects of rearranging , but I am asking if there is any direct way ( compiler option ) to turn this off ?

  • 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-27T23:34:00+00:00Added an answer on May 27, 2026 at 11:34 pm

    The javac compiler has performs next to no optimisations.

    The JIT native compiler could re-order instructions where there is a memory ordering problem. However, the CPU also can re-order instructions and memory updates which have the same effect.

    What is achieved by allowing this freedm to the compiler ?

    The main benefit is code portability. The more guarantees you provide, the more difficult it is to ensure every platform actually does this.

    There is also a significant performance improvement by allowing the CPU to execute instructions as and when it can rather than in a strict order.

    Is it really possible for the compiler to produce code which is more efficient by rearranging the code ?

    Yes. but the re-ordering done by the CPU is more significant.

    I am yet to see a practical case for this. I feel sometimes that the benefits if any are far outweighed by the concurrency risks this can introduce.

    Is there any way that programmer can tell compiler not to rearrange lines like this ?

    This is why you use memory barriers like volatile, synchronized blocks and Lock. When you use these you get thread safety guarantees.

    I know that using synchronization primitives effectively handles the side-effects of rearranging , but I am asking if there is any direct way ( compiler option ) to turn this off ?

    You can turn off the JIT, but most re-ordering is done by the CPU so it wouldn’t achieve much.

    Avoiding re-ordering of updates is such a small part of the thread safety problem (its biggest issue is that is obscure and rarely occurs which makes testing it hard) And once you write thread safe code, this is alleviated.

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

Sidebar

Related Questions

It is well-known that monoids are stunningly ubiquitous in programing. They are so ubiquitous
I can't seem to find an answer to this anywhere. It's well known that
Its a well known fact that a static method can work only on static
I am wondering if there are any well-known algorithms that I should be aware
It's a well known fact, that Oracle treats empty strings as null. However, I'm
I find that many high level functions are missing in most well-known javascript libraries
I have used Java, C++, .Net. (in that order). When asked about by-value vs.
I need to create a porting software that will convert Java code to C#
It'a a well-known fact that UML does not Turing complete (in contrast to usual
As well-known, C++ has steeper learning curve than most of the mainstream languages, which

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.