Unsafe: removing the monitorEnter/monitorExit/tryMonitorEnter methods

Mandy Chung mandy.chung at oracle.com
Wed Feb 26 23:33:11 UTC 2014


On 2/26/2014 12:54 PM, Martin Buchholz wrote:
> I don't recall having added this particular wart
> to test/java/lang/ProcessBuilder/Basic.java, and history suggests that
> others did that.
>
> It does seem that being able to tell whether a java object monitor is
> currently locked is useful for debugging and monitoring - there should be a
> way to do that.

The closest one I can think of is java.lang.management.ThreadMXBean that 
can find what object monitors are locked and you can compare the 
LockInfo with the classname and identity hash of the object you are 
interested in.  However, it means that the test would have a dependency 
on the management module.

  ThreadMXBean tmxbean = ManagementFactory.getThreadMXBean();
  ThreadInfo[] tinfos = tmxbean.dumpAllThreads(true, false);

ThreadInfo.getLockedMonitors() returns the locked object monitors.

Mandy

>
> On Wed, Feb 26, 2014 at 7:12 AM, Paul Sandoz <paul.sandoz at oracle.com> 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.
>>




More information about the core-libs-dev mailing list