I have never used this "in the wild", but rather have moved over to the
ReentrantLock when I needed that particular functionality.
However, I do see a place for this ability. As I wrote in here:
http://www.javaspecialists.eu/archive/Issue194.html - sometimes you need
to modify code that is already using a particular locking mechanism. To
then redo everything with ReentrantLock can introduce errors.
If I had a say, I would vote to either keep that method in Unsafe (which
is where I think it belongs) or provide an alternative way to make the
tryMonitorEnter() mechanism available to those that might end up needing it.
Since I bought my Suzuki Jimny 7 years ago, I have not needed my spare
wheel even once. So yeah, I could take it off and drive around without
it. But chances are, the day after I take it off, I will need it.
tryMonitorEnter() does no harm and it is out of reach of most programmers.
Regards
Heinz
--
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Oracle Java Champion 2005-2013
JavaOne Rock Star Speaker 2012
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz
Paul Sandoz wrote:
Hi,
Out of all the methods on Unsafe i think the
monitorEnter/monitorExit/tryMonitorEnter are the least used and are very strong
candidates for removal.
99% of use-cases are supported by classes in the java.util.concurrent.locks
package.
Within the JDK itself it is only used in one JDK test file
test/java/lang/ProcessBuilder/Basic.java:
while (unsafe.tryMonitorEnter(s)) {
unsafe.monitorExit(s);
Thread.sleep(1);
}
for a test verifying an EOF is received on pending reads and it is polling to
check when the process builder acquires the lock before destroying the process,
presumably to avoid some sort of race condition that occasionally causes the
test to fail.
I believe this test as been through a number of rounds, i stared at things for
a bit, but cannot quickly work out a replacement; i lack the knowledge on this
area.
Outside of the JDK i can only find one usage of monitorExit/Enter (according to
grep code) in JBoss modules, and i believe this is only used on Java 1.6 to
work around some limitations in class loading that were fixed in 1.7.
Given such very limited use i propose to remove these methods after having
worked out a fix for ProcessBuilder/Basic.java test.
Paul.