Note that there are some explanatory texts on larger screens.

plurals
  1. POInterpretation of "program order rule" in Java concurrency
    primarykey
    data
    text
    <p>Program order rule states "Each action in a thread happens-before every action in that thread that comes later in the program order"</p> <p>1.I read in another <a href="https://stackoverflow.com/questions/11353274/what-is-the-reasoning-behind-volatile-semantics-in-java-and-c-sharp">thread</a> that an <strong>action</strong> is</p> <ul> <li>reads and writes to variables</li> <li>locks and unlocks of monitors</li> <li>starting and joining with threads </li> </ul> <p>Does this mean that reads and writes can be changed in order, but reads and writes cannot change order with actions specified in 2nd or 3rd lines?</p> <p>2.What does "program order" mean? </p> <p>Explanation with an examples would be really helpful.</p> <p><strong>Additional related question</strong></p> <p>Suppose I have the following code:</p> <pre><code>long tick = System.nanoTime(); //Line1: Note the time //Block1: some code whose time I wish to measure goes here long tock = System.nanoTime(); //Line2: Note the time </code></pre> <p>Firstly, it's a single threaded application to keep things simple. Compiler notices that it needs to check the time twice and also notices a block of code that has no dependency with surrounding time-noting lines, so it sees a potential to reorganize the code, which could result in Block1 not being surrounded by the timing calls during actual execution (for instance, consider this order Line1->Line2->Block1). But, I as a programmer can see the dependency between Line1,2 and Block1. Line1 should immediately precede Block1, Block1 takes a finite amount of time to complete, and immediately succeeded by Line2.</p> <p>So my question is: Am I measuring the block correctly? </p> <ul> <li>If yes, what is preventing the compiler from rearranging the order.</li> <li>If no, (which is think is correct after going through Enno's answer) what can I do to prevent it.</li> </ul> <p>P.S.: I stole this code from <a href="https://stackoverflow.com/questions/15607265/measuring-time-interval-and-out-of-order-execution">another question</a> I asked in SO recently.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload