JDK-6712222 : Race condition in java/lang/management/ThreadMXBean/AllThreadIds.java
  • Type: Bug
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Affected Version: 6u6,7,9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic,x86,sparc
  • Submitted: 2008-06-09
  • Updated: 2015-09-29
  • Resolved: 2015-03-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u60Fixed 9 b56Fixed
Description
Test
	java/lang/management/ThreadMXBean/AllThreadIds.java

uses Barrier to signal that thread has exited:

    228             barrier.signal();
    229         }

However, there is small window between call to .signal() and actual thread completion when main thread may observe wrong number of finished threads.

Comments
Unfortunately, the race condition is inevitable. It is not possible to hook into the thread's lifecycle to get a notification once the thread stopped its execution. One may try using thread.join() method to wait for a particular thread to die but in order to be able to use this technique one would need to spawn additional threads thus increasing the probability of confusing the expected test results. Waiting for a certain period before trying to get the number of live threads seems to be the most feasible way to minimize the problem given the existing APIs.
22-07-2013