1 /* 2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 package nsk.monitoring.share.thread; 24 25 import nsk.share.log.Log; 26 import java.lang.management.ThreadInfo; 27 import java.lang.management.MonitorInfo; 28 import java.lang.management.LockInfo; 29 30 /** 31 * WaitingThread is RecursiveMonitoringThread that is waiting on a lock. 32 */ 33 public class WaitingThread extends RecursiveMonitoringThread { 34 private String lock = new String("a lock"); 35 private volatile boolean ready = false; 36 private Object readyLock = new Object(); 37 private static final String[] expectedMethods = { 38 "nsk.monitoring.share.thread.WaitingThread.runInside", 39 "java.lang.Object.wait" 40 }; 41 WaitingThread(Log log, RunType recursionType, int maxDepth)42 public WaitingThread(Log log, RunType recursionType, int maxDepth) { 43 super(log, recursionType, maxDepth); 44 } 45 checkThreadInfo(ThreadInfo info)46 public void checkThreadInfo(ThreadInfo info) { 47 super.checkThreadInfo(info); 48 verify(info.getThreadState() == Thread.State.WAITING, "ThreadInfo.getThreadState() = " + info.getThreadState() + " != " + Thread.State.WAITING); 49 verify(info.getBlockedTime() == 0 || info.getBlockedTime() == -1, "ThreadInfo.getBlockedTime() == " + info.getBlockedTime()); 50 verify(info.getBlockedCount() >= 0, "ThreadInfo.getBlockedCount() = " + info.getBlockedCount() + " != 1"); 51 verify(info.getWaitedTime() > 0 || info.getWaitedTime() == -1, "ThreadInfo.getWaitedTime() == " + info.getWaitedTime()); 52 verify(info.getWaitedCount() == 1, "ThreadInfo.getWaitedCount() = " + info.getWaitedCount() + " != 1"); 53 checkLockInfo(info.getLockInfo()); 54 verify(info.getLockName().equals(info.getLockInfo().toString()), "ThreadInfo.getLockName() = " + info.getLockName() + " != ThreadInfo.getLockInfo().toString() = " + info.getLockInfo().toString()); 55 verify(info.getLockOwnerId() == -1, "ThreadInfo.getLockOwnerId() = " + info.getLockOwnerId() + " != -1"); 56 verify(info.getLockOwnerName() == null , "ThreadInfo.getLockOwnerName() = " + info.getLockOwnerName() + " != null"); 57 checkMonitorInfo(info.getLockedMonitors(), null); 58 checkSynchronizers(info.getLockedSynchronizers(), null); 59 } 60 checkLockInfo(LockInfo lockInfo)61 private void checkLockInfo(LockInfo lockInfo) { 62 verify(lockInfo.getClassName().equals(lock.getClass().getName()), "LockInfo.getClassName() = " + lockInfo.getClassName() + " differs from lock.getClass().getName() = " + lock.getClass().getName()); 63 verify(lockInfo.getIdentityHashCode() == System.identityHashCode(lock), "LockInfo.getIdentityHashCode() = " + lockInfo.getIdentityHashCode() + " differs from System.identityHashCode(lock) = " + System.identityHashCode(lock)); 64 String expectedToString = lock.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(lock)); 65 verify(lockInfo.toString().equals(expectedToString), "LockInfo.toString() = " + lockInfo.toString() + " differs from expected toString() = " + expectedToString); 66 } 67 waitState()68 public void waitState() { 69 synchronized (readyLock) { 70 while (!ready) { 71 try { 72 readyLock.wait(); 73 } catch (InterruptedException e) { 74 log.warn(e); 75 } 76 } 77 } 78 waitThreadState(Thread.State.WAITING); 79 } 80 finish()81 public void finish() { 82 ready = false; 83 synchronized (lock) { 84 lock.notifyAll(); 85 } 86 } 87 runInside()88 protected void runInside() { 89 synchronized (readyLock) { 90 ready = true; 91 readyLock.notifyAll(); 92 } 93 while (ready) { 94 synchronized (lock) { 95 try { 96 lock.wait(); 97 } catch (InterruptedException e) { 98 log.warn(e); 99 } 100 } 101 } 102 } 103 isStackTraceElementExpected(StackTraceElement element)104 protected boolean isStackTraceElementExpected(StackTraceElement element) { 105 return super.isStackTraceElementExpected(element) || checkStackTraceElement(element, expectedMethods); 106 } 107 } 108