diff options
| author | 2017-01-13 11:29:41 +0000 | |
|---|---|---|
| committer | 2017-01-13 14:18:42 +0000 | |
| commit | 91011afe8ed113b02878a4186fc103eaaef3c102 (patch) | |
| tree | 2680953b28e96dbd7d37b5511001ce6734ee3542 | |
| parent | fd3f4e7d340c77d2d3f4de17a28e768eb4d82045 (diff) | |
Make 129-ThreadGetId more robust.
Make sure we get the heap task daemon, to avoid a race with the runtime
creating the stack trace, and then updating the peer.
test: 129-ThreadGetId
bug: 28261069
Change-Id: I739ab6cd0180e2be07b7cecac6ad8a905a7c9cd0
| -rw-r--r-- | test/129-ThreadGetId/src/Main.java | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/test/129-ThreadGetId/src/Main.java b/test/129-ThreadGetId/src/Main.java index 5aefd17f0e..6ba01ff0b5 100644 --- a/test/129-ThreadGetId/src/Main.java +++ b/test/129-ThreadGetId/src/Main.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import java.lang.reflect.Field; import java.util.Map; public class Main implements Runnable { @@ -33,14 +34,38 @@ public class Main implements Runnable { System.out.println("Finishing"); } - static void test_getStackTraces() { + static Thread getHeapTaskDaemon() throws Exception { + Field f = ThreadGroup.class.getDeclaredField("systemThreadGroup"); + f.setAccessible(true); + ThreadGroup systemThreadGroup = (ThreadGroup) f.get(null); + + while (true) { + int activeCount = systemThreadGroup.activeCount(); + Thread[] array = new Thread[activeCount]; + systemThreadGroup.enumerate(array); + for (Thread thread : array) { + if (thread.getName().equals("HeapTaskDaemon") && + thread.getState() != Thread.State.NEW) { + return thread; + } + } + // Yield to eventually get the daemon started. + Thread.sleep(10); + } + } + + static void test_getStackTraces() throws Exception { + Thread heapDaemon = getHeapTaskDaemon(); + + // Force a GC to ensure the daemon truly started. + Runtime.getRuntime().gc(); // Check all the current threads for positive IDs. Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces(); for (Map.Entry<Thread, StackTraceElement[]> pair : map.entrySet()) { Thread thread = pair.getKey(); // Expect empty stack trace since we do not support suspending the GC thread for // obtaining stack traces. See b/28261069. - if (thread.getName().equals("HeapTaskDaemon")) { + if (thread == heapDaemon) { System.out.println(thread.getName() + " depth " + pair.getValue().length); } } |