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