multithreading - Java - happens-before relationship for monitor unlock -


I've read recently, which clearly describes lots of intrinsics in the Java Memory model of a special component My attention is:

  The rule for a monetrexit (i.e., issuing a synchronization) is that before the monitors can be released, the action should be taken prior to monitérecite.  

I understand, however, before reading and what happens, I could get about the monitor unblock, when a thread unlocks the monitor which is First The second thread is locking it again (which also makes sense). Can anyone explain that how does JLS clarify the situation that all tasks within the synchronization block should be unlocked before the operation?

More comments:

I want to be based on some reactions

Cited:

  a = new A ()  

If new A () , address on one heap for the next work a , the compiler can re-order the people who have the Hep address a and then follow the normal initialization (problem with double check locking)

  Sync Niz {a = 5; } Print A;  

can be changed to

  synchronize {a = 5}; Print A; }  

So we have recharged monitrecit with print statement (also valid as JLS)

Now, the simple matter I Mentioned:

  x = 1; Y = 2; C = x + y; Print C;  

I do not see any reason that prevents a compiler either specifying x or y first. Nothing is completely stopping in it, because the final output is unchanged regardless of whether the X is assigned before or y, the re-ordering is completely possible.

  1. Monitor. Unlock

On the basis of example with the "pulled" print statement in the synchronization block, let's try it on the opposite

  synchronize {a = 5; Print A; }  

I can expect the compiler to do this:

  synchronize {a = 5; } Print A;  

It looks absolutely right in a single-threaded world, right now This is definitely invalid, and against JLS (according to the quoted source). Now that's the case, if I did not get anything in this JLS? And obviously, the motivation about "the sequence of events" is now irrelevant, because the compiler can do rearranging "statements" in the syndrome block.

This is the only synchronized block Not even this, monitorexit .

Can anyone explain how JLS clearly states how all actions within the synchronization block should be unlocked before the operation?

Regardless of synchronize , a specific thread (and only one thread) maintains the program sequence, so it appears that all the reads and (We do not need to do this-before the single thread case before ordering)

Many threads are kept in mind before the relationship, which will A thread is happening before monitorexit, all of them A constant monitorenter .

Edit to address your updates.

There are some rules that the compiler should follow according to sequence again.

There are especially useful entries

  • First task
  • Second Action : Exit monitor

Here the value is No which means that the compiler can not rearrange two functions in which the first normal load occurs and the second < Code> Monetrexit , so in your case it will be repeated in violation of JLS.

A rule known as Roach-Motel order, which reads / writes can be done again in the synchronized block but not out of it.


Comments