/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4530538
* @summary Basic unit test of the synchronization statistics support:
*
* @author Mandy Chung
*
* @ignore 6309226
* @build Semaphore
* @run main SynchronizationStatistics
*/
public class SynchronizationStatistics {
private static boolean blockedTimeCheck =
private static boolean trace = false;
private static volatile boolean testFailed = false;
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
}
trace = true;
}
if (blockedTimeCheck) {
}
if (!mbean.isThreadContentionMonitoringEnabled()) {
throw new RuntimeException("TEST FAILED: " +
"Thread Contention Monitoring is not enabled");
}
// Start the threads and check them in Blocked and Waiting states
// wait until the examiner acquires all the locks and waiting
// for the BlockedThread to start
"is waiting to begin.");
// The Examiner should be waiting to be notified by the BlockedThread
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
if (testFailed)
throw new RuntimeException("TEST FAILED.");
}
if (t != null) {
}
}
}
throws Exception {
if (ti.getThreadState() != s) {
throw new RuntimeException("TEST FAILED: " +
". Expected to be " + s);
}
}
throws Exception {
throw new RuntimeException("TEST FAILED: " +
}
}
super(name);
}
}
}
}
long totalBlockedTimeMs() {
return totalBlockedEnterTime / 1000000;
}
return t / 1000000;
}
long totalWaitTimeMs() {
return totalWaitTime / 1000000;
}
return t / 1000000;
}
}
super(name);
}
void waitUntilBlocked() {
// give a chance for the examiner thread to really wait
goSleep(20);
}
void waitUntilWaiting() {
}
boolean hasWaitersForBlocked() {
}
private void notifyWaiter() {
// wait until the examiner waits on the semaphore
goSleep(20);
}
}
synchronized (waiter) {
try {
// notify examinerabout to wait on a monitor
notifyWaiter();
} catch (Exception e) {
e.printStackTrace();
testFailed = true;
}
}
}
private void test() {
// notify examiner about to block on lockA
notifyWaiter();
synchronized (lockA) {
A(); // Expected blocked count = 1
}
E();
}
private void A() {
// notify examiner about to block on lockB
notifyWaiter();
synchronized (lockB) {
B(); // Expected blocked count = 2
}
}
private void B() {
// notify examiner about to block on lockC
notifyWaiter();
synchronized (lockC) {
C(); // Expected blocked count = 3
}
}
private void C() {
// notify examiner about to block on lockD
notifyWaiter();
synchronized (lockD) {
D(); // Expected blocked count = 4
}
}
private void D() {
goSleep(50);
}
private void E() {
final int WAIT = 1000;
}
public void run() {
test();
} // run()
} // BlockedThread
super(name);
}
}
long blockedTime,
long nowNano)
throws Exception {
// accept 5% range
}
long waitedTime,
long nowNano)
throws Exception {
// accept 5% range
}
throws Exception {
if (trace) {
" expected = " + expected +
". Diff = " + diff);
}
// throw an exception if blockedTime and expectedTime
// differs > percent%
if (diff < 0) {
}
// minimum range = 2 ms
if (range < 2) {
range = 2;
}
throw new RuntimeException("TEST FAILED: " +
"Time returned = " + time +
}
}
throws Exception {
action = "wait on ";
action = "block on ";
}
" with blocked count = " + bcount +
" and waited count = " + wcount);
if (info.getThreadState() != s) {
throw new RuntimeException("TEST FAILED: " +
". Expected to be " + s);
}
throw new RuntimeException("TEST FAILED: " +
}
throw new RuntimeException("TEST FAILED: " +
". Expected to be " + bcount);
}
throw new RuntimeException("TEST FAILED: " +
". Expected to be " + wcount);
}
throw new RuntimeException("TEST FAILED: " +
"Object blocked on is " + lockObj +
}
if (!blockedTimeCheck) {
return;
}
if (blockedTime < 0) {
throw new RuntimeException("TEST FAILED: " +
"Blocked time returned is negative = " + blockedTime);
}
} else {
}
if (waitedTime < 0) {
throw new RuntimeException("TEST FAILED: " +
"Waited time returned is negative = " + waitedTime);
}
} else {
}
}
private void examine() {
try {
synchronized (lockD) {
synchronized (lockC) {
synchronized (lockB) {
synchronized (lockA) {
// notify main thread to continue
// wait until BlockedThread has started
blockedCount++;
lockA, "lockA",
}
// wait until BlockedThread to block on lockB
blockedCount++;
lockB, "lockB",
}
// wait until BlockedThread to block on lockC
blockedCount++;
lockC, "lockC",
}
// wait until BlockedThread to block on lockD
blockedCount++;
lockD, "lockD",
}
// wait until BlockedThread about to call E()
// BlockedThread will wait on waiter for 3 times
waitedCount++;
waitedCount++;
waitedCount++;
} catch (Exception e) {
e.printStackTrace();
testFailed = true;
}
}
public void run() {
examine();
} // run()
public void waitUntilWaiting() {
// wait until the examiner is waiting for
while (!blockedThread.hasWaitersForBlocked()) {
goSleep(50);
}
// give a chance for the examiner thread to really wait
goSleep(20);
}
} // Examiner
}