< prev index next >

jdk/test/java/lang/management/ThreadMXBean/Locks.java

Print this page
rev 16982 : imported patch 8180399


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4530538
  27  * @summary Basic unit test of ThreadInfo.getLockName()
  28  *          and ThreadInfo.getLockOwnerName()
  29  * @author  Mandy Chung
  30  * @author  Jaroslav Bachorik
  31  *
  32  * @library /lib/testlibrary
  33  *
  34  * @build jdk.testlibrary.*
  35  * @run main/othervm Locks
  36  */
  37 import java.lang.management.*;
  38 import java.util.Arrays;

  39 import java.util.Optional;
  40 import java.util.concurrent.Phaser;
  41 import java.util.function.Predicate;
  42 import jdk.testlibrary.LockFreeLogManager;
  43 
  44 public class Locks {
  45 
  46     private static final Object OBJA = new Object();
  47     private static final Object OBJB = new Object();
  48     private static final EnhancedWaiter OBJC = new EnhancedWaiter();
  49     private static final ThreadMXBean TM = ManagementFactory.getThreadMXBean();
  50     private static final LockFreeLogManager LOGGER = new LockFreeLogManager();
  51 
  52     private static String getLockName(Object lock) {
  53         if (lock == null) return null;
  54 
  55         return lock.getClass().getName() + '@' +
  56                 Integer.toHexString(System.identityHashCode(lock));
  57     }
  58 
  59     private static void assertNoLock(Thread t) {
  60         if (t == null) {
  61             return;
  62         }
  63         Optional<ThreadInfo> result = Arrays.asList(
  64                 TM.getThreadInfo(TM.getAllThreadIds(), true, true)).
  65                 stream().
  66                 filter(tInfo -> (tInfo != null && tInfo.getLockOwnerName() != null)
  67                         ? tInfo.getLockOwnerName().equals(t.getName()) : false).
  68                 findAny();
  69         if (result.isPresent()) {
  70             throw new RuntimeException("Thread " + t.getName() + " is not "
  71                     + "supposed to be hold any lock. Currently owning lock : "
  72                     + result.get().getLockName());
  73         }
  74     }
  75 
  76    /*
  77     * Handy debug function to check if error condition is because of test code or not.
  78     */
  79     private static void printStackTrace(Thread thread) {
  80         if (thread == null) {
  81             return;
  82         }
  83         StackTraceElement[] stackTrace = thread.getStackTrace();
  84         log("Stack dump : Thread -> " + thread.getName());
  85         for (StackTraceElement stackTraceEl : stackTrace) {
  86             log("\t" + stackTraceEl.toString());
  87         }
  88     }




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4530538
  27  * @summary Basic unit test of ThreadInfo.getLockName()
  28  *          and ThreadInfo.getLockOwnerName()
  29  * @author  Mandy Chung
  30  * @author  Jaroslav Bachorik
  31  *
  32  * @library /lib/testlibrary /test/lib
  33  *
  34  * @build jdk.testlibrary.*
  35  * @run main/othervm Locks
  36  */
  37 import java.lang.management.*;
  38 import java.util.Arrays;
  39 import java.util.Objects;
  40 import java.util.Optional;
  41 import java.util.concurrent.Phaser;
  42 import java.util.function.Predicate;
  43 import jdk.test.lib.log.LockFreeLogManager;
  44 
  45 public class Locks {
  46 
  47     private static final Object OBJA = new Object();
  48     private static final Object OBJB = new Object();
  49     private static final EnhancedWaiter OBJC = new EnhancedWaiter();
  50     private static final ThreadMXBean TM = ManagementFactory.getThreadMXBean();
  51     private static final LockFreeLogManager LOGGER = new LockFreeLogManager();
  52 
  53     private static String getLockName(Object lock) {
  54         if (lock == null) return null;
  55 
  56         return lock.getClass().getName() + '@' +
  57                 Integer.toHexString(System.identityHashCode(lock));
  58     }
  59 
  60     private static void assertNoLock(Thread t) {
  61         if (t == null) {
  62             return;
  63         }
  64         String name = t.getName();
  65         Optional<ThreadInfo> result = Arrays.stream(
  66                 TM.getThreadInfo(TM.getAllThreadIds(), true, true))
  67                                             .filter(Objects::nonNull)
  68                                             .filter(i -> name.equals(i.getLockOwnerName()))
  69                                             .findAny();
  70         if (result.isPresent()) {
  71             throw new RuntimeException("Thread " + t.getName() + " is not "
  72                     + "supposed to be hold any lock. Currently owning lock : "
  73                     + result.get().getLockName());
  74         }
  75     }
  76 
  77    /*
  78     * Handy debug function to check if error condition is because of test code or not.
  79     */
  80     private static void printStackTrace(Thread thread) {
  81         if (thread == null) {
  82             return;
  83         }
  84         StackTraceElement[] stackTrace = thread.getStackTrace();
  85         log("Stack dump : Thread -> " + thread.getName());
  86         for (StackTraceElement stackTraceEl : stackTrace) {
  87             log("\t" + stackTraceEl.toString());
  88         }
  89     }


< prev index next >