Java LockSupport Memory Maintenance

Java 6 API Question. Does a call to LockSupport.unpark(thread) happen - before regards to returning from LockSupport.park in a simple-unarmed thread? I strongly suspect the answer is yes, but Javadoc does not seem to explicitly mention this.

+7
java api concurrency memory-barriers java-memory-model
source share
3 answers

I looked at the JDK code, and it looks like LockSupport methods usually call outside synchronization blocks. So your guess seems right.

+1
source share

I just found this question because I asked myself the same thing. According to this article by Oracle researcher David Dice , the answer seems no . Here is the relevant part of the article:

If the stream is blocked in park() , we guarantee that subsequent unpark() will make it ready. A completely legal, but poor-quality implementation of park() and unpark() will be an empty method in which the program degenerates into a simple spinning. In fact, the litmus test for the correct use of park() is unpark() .

The empty park() and unpark() methods do not give you any action - before the relations guarantee, so for your program, which will be 100% portable, you should not rely on them.

Then again, Javadoc LockSupport says:

These methods are intended to be used as tools for creating higher-level synchronization utilities and are not in themselves useful for most concurrency management applications. The park method is intended for use only in constructions of the form:

while (!canProceed()) { ... LockSupport.park(this); }

Since you must explicitly check for a condition in any case that will either include volatile or correctly synchronized variables, then weak park() guarantees should not be a problem, right?

+6
source share

If it is not documented as such, you CANNOT rely on it to create what happened before the relationship.

In particular, LockSupport.java in the Hotspot code simply calls Unsafe.park and .unpark!

The connection between events and events usually comes from a reading pair with a record of the state of volatility on the flag or something like that.

Remember that if it is not documented as creating a connection between events, then you should treat it as if it did not even prove that it operates in your particular system. Future systems and implementations cannot. They left themselves this freedom for a good reason.

+5
source share

All Articles