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 * SleepingThread is RecursiveMonitoringThread that sleeps. 32 */ 33 public class SleepingThread extends RecursiveMonitoringThread { 34 private String lock = new String("a lock"); 35 private volatile boolean sleeping = false; 36 private Object readyLock = new Object(); 37 private static final String[] expectedMethods = { 38 "java.lang.Thread.sleep", 39 "nsk.monitoring.share.thread.SleepingThread.runInside" 40 }; 41 SleepingThread(Log log, RunType recursionType, int maxDepth)42 public SleepingThread(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.TIMED_WAITING, "ThreadInfo.getThreadState() = " + info.getThreadState() + " != " + Thread.State.TIMED_WAITING); 49 verify(info.getBlockedTime() == 0 || info.getBlockedTime() == -1, "ThreadInfo.getBlockedTime() == " + info.getBlockedTime()); 50 verify(info.getBlockedCount() >= 0, "ThreadInfo.getBlockedCount() = " + info.getBlockedCount() + " != 0"); 51 verify(info.getWaitedTime() == 0 || info.getWaitedTime() == -1, "ThreadInfo.getWaitedTime() == " + info.getWaitedTime()); 52 verify(info.getWaitedCount() == 1, "ThreadInfo.getWaitedCount() = " + info.getWaitedCount() + " != 1"); 53 verify(info.getLockInfo() == null, "ThreadInfo.getLockInfo() != null "); 54 verify(info.getLockName() == null, "ThreadInfo.getLockName() " + info.getLockName() + " != null"); 55 verify(info.getLockOwnerId() == -1, "ThreadInfo.getLockOwnerId() = " + info.getLockOwnerId() + " != null"); 56 verify(info.getLockOwnerName() == null, "ThreadInfo.getLockOwnerName() = " + info.getLockOwnerName() + " != null"); 57 checkMonitorInfo(info.getLockedMonitors(), null); 58 checkSynchronizers(info.getLockedSynchronizers(), null); 59 } 60 waitState()61 public void waitState() { 62 synchronized (readyLock) { 63 while (!sleeping) { 64 try { 65 readyLock.wait(); 66 } catch (InterruptedException e) { 67 log.warn(e); 68 } 69 } 70 } 71 waitThreadState(Thread.State.TIMED_WAITING); 72 } 73 finish()74 public void finish() { 75 sleeping = false; 76 runner.interrupt(); 77 } 78 runInside()79 protected void runInside() { 80 synchronized (readyLock) { 81 sleeping = true; 82 readyLock.notifyAll(); 83 } 84 while (sleeping) { 85 try { 86 Thread.sleep(10000000); 87 } catch (InterruptedException e) { 88 } 89 } 90 } 91 isStackTraceElementExpected(StackTraceElement element)92 protected boolean isStackTraceElementExpected(StackTraceElement element) { 93 return super.isStackTraceElementExpected(element) || checkStackTraceElement(element, expectedMethods); 94 } 95 } 96