From 9429412d5bad3df423eeb6f3299e2298b90b9c0d Mon Sep 17 00:00:00 2001 From: jbachorik Date: Tue, 1 Jul 2014 11:47:36 +0200 Subject: [PATCH] 8038794: java/lang/management/ThreadMXBean/SynchronizationStatistics.java fails intermittently Reviewed-by: sla --- .../SynchronizationStatistics.java | 133 ++++++++++++------ 1 file changed, 88 insertions(+), 45 deletions(-) diff --git a/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java b/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java index 6986adc68..f998b7e23 100644 --- a/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java +++ b/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java @@ -34,6 +34,7 @@ import java.lang.management.*; import java.util.concurrent.Phaser; +import java.util.function.Supplier; public class SynchronizationStatistics { private static class LockerThread extends Thread { @@ -86,6 +87,8 @@ public class SynchronizationStatistics { private static void testBlockingOnSimpleMonitor() throws Exception { System.out.println("testBlockingOnSimpleMonitor"); final Object lock1 = new Object(); + System.out.println("Lock1 = " + lock1); + final Phaser p = new Phaser(2); LockerThread lt = newLockerThread(new Runnable() { @Override @@ -110,12 +113,12 @@ public class SynchronizationStatistics { } p.arriveAndAwaitAdvance(); // phase[2] - testBlocked(ti, mbean.getThreadInfo(tid), lockName, lock1); + testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1); p.arriveAndDeregister(); // phase[3] lt.join(); - System.out.println("OK"); + printok(); } /** @@ -128,6 +131,9 @@ public class SynchronizationStatistics { final Object lock1 = new Object(); final Object lock2 = new Object(); + System.out.println("Lock1 = " + lock1); + System.out.println("Lock2 = " + lock2); + final Phaser p = new Phaser(2); LockerThread lt = newLockerThread(new Runnable() { @Override @@ -149,7 +155,6 @@ public class SynchronizationStatistics { lt.start(); long tid = lt.getId(); ThreadInfo ti = mbean.getThreadInfo(tid); - ThreadInfo ti1 = null; String lockName = null; synchronized(lock1) { p.arriveAndAwaitAdvance(); // phase[1] @@ -158,9 +163,7 @@ public class SynchronizationStatistics { } p.arriveAndAwaitAdvance(); // phase[2] - ti1 = mbean.getThreadInfo(tid); - testBlocked(ti, ti1, lockName, lock1); - ti = ti1; + ti = testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1); synchronized(lock2) { p.arriveAndAwaitAdvance(); // phase [3] @@ -168,12 +171,12 @@ public class SynchronizationStatistics { lockName = mbean.getThreadInfo(tid).getLockName(); } p.arriveAndAwaitAdvance(); // phase [4] - testBlocked(ti, mbean.getThreadInfo(tid), lockName, lock2); + testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock2); p.arriveAndDeregister(); lt.join(); - System.out.println("OK"); + printok(); } /** @@ -209,13 +212,12 @@ public class SynchronizationStatistics { } p.arriveAndAwaitAdvance(); // phase[2] - ThreadInfo ti2 = mbean.getThreadInfo(lt.getId()); + testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1); p.arriveAndDeregister(); // phase[3] lt.join(); - testWaited(ti1, ti2, 1); - System.out.println("OK"); + printok(); } /** @@ -256,12 +258,12 @@ public class SynchronizationStatistics { int phase = p.getPhase(); while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4] - ThreadInfo ti2 = mbean.getThreadInfo(lt.getId()); + testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3); p.arriveAndDeregister(); // phase[5] lt.join(); - testWaited(ti1, ti2, 3); - System.out.println("OK"); + + printok(); } /** @@ -331,44 +333,85 @@ public class SynchronizationStatistics { } p.arriveAndAwaitAdvance(); // phase[4] - ThreadInfo ti2 = mbean.getThreadInfo(lt.getId()); + testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3); p.arriveAndDeregister(); // phase[5] lt.join(); - testWaited(ti1, ti2, 3); - System.out.println("OK"); + printok(); } - private static void testWaited(ThreadInfo ti1, ThreadInfo ti2, int waited) throws Error { - long waitCntDiff = ti2.getWaitedCount() - ti1.getWaitedCount(); - long waitTimeDiff = ti2.getWaitedTime() - ti1.getWaitedTime(); - if (waitCntDiff < waited) { - throw new Error("Unexpected diff in waited count. Expecting at least " - + waited + " , got " + waitCntDiff); - } - if (waitTimeDiff <= 0) { - throw new Error("Unexpected diff in waited time. Expecting increasing " + - "value, got " + waitTimeDiff + "ms"); - } + private static void printok() { + System.out.println("OK\n"); } - private static void testBlocked(ThreadInfo ti1, ThreadInfo ti2, + private static void testWaited(ThreadInfo ti1, Supplier ti2, int waited) + throws InterruptedException { + boolean error; + do { + error = false; + ThreadInfo ti = ti2.get(); + long waitCntDiff = ti.getWaitedCount() - ti1.getWaitedCount(); + long waitTimeDiff = ti.getWaitedTime() - ti1.getWaitedTime(); + if (waitCntDiff < waited) { + System.err.println( + "Unexpected diff in waited count. Expecting at least " + + waited + " , got " + waitCntDiff + ); + error = true; + } + if (waitTimeDiff <= 0) { + System.err.println( + "Unexpected diff in waited time. Expecting increasing " + + "value, got " + waitTimeDiff + "ms" + ); + error = true; + } + if (error) { + System.err.println("Retrying in 20ms ..."); + Thread.sleep(20); + } + } while (error); + } + + private static ThreadInfo testBlocked(ThreadInfo ti1, Supplier ti2, String lockName, final Object lock) - throws Error { - long blkCntDiff = ti2.getBlockedCount() - ti1.getBlockedCount(); - long blkTimeDiff = ti2.getBlockedTime() - ti1.getBlockedTime(); - if (blkCntDiff < 1) { - throw new Error("Unexpected diff in blocked count. Expecting at least 1, " + - "got " + blkCntDiff); - } - if (blkTimeDiff < 0) { - throw new Error("Unexpected diff in blocked time. Expecting a positive " + - "number, got " + blkTimeDiff); - } - if (!lockName.equals(lock.toString())) { - throw new Error("Unexpected blocked monitor name. Expecting " + - lock.toString() + ", got " + - lockName); - } + throws InterruptedException { + boolean error; + ThreadInfo ti = null; + do { + error = false; + ti = ti2.get(); + long blkCntDiff = ti.getBlockedCount() - ti1.getBlockedCount(); + long blkTimeDiff = ti.getBlockedTime() - ti1.getBlockedTime(); + + System.out.println("testBlocked: [" + blkCntDiff + ", " + blkTimeDiff + ", " + lockName + "]"); + + if (blkCntDiff < 1) { + System.err.println( + "Unexpected diff in blocked count. Expecting at least 1, " + + "got " + blkCntDiff + ); + error = true; + } + if (blkTimeDiff < 0) { + System.err.println( + "Unexpected diff in blocked time. Expecting a positive " + + "number, got " + blkTimeDiff + ); + error = true; + } + if (!lockName.equals(lock.toString())) { + System.err.println( + "Unexpected blocked monitor name. Expecting " + + lock.toString() + ", got " + lockName + ); + error = true; + } + if (error) { + System.err.println("Retrying in 20ms ..."); + Thread.sleep(20); + } + } while (error); + return ti; } } -- GitLab