Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/MyOwnSynchronizer.java
41152 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 5086470 635824726* @summary Basic Test for ThreadMXBean.dumpAllThreads(false, true)27* and getThreadInfo of customized JSR-166 synchronizers.28* @author Mandy Chung29*30* @build Barrier31* @build ThreadDump32* @run main/othervm MyOwnSynchronizer33*/3435import java.lang.management.*;36import java.util.*;37import java.util.concurrent.locks.*;38import java.util.concurrent.TimeUnit;39import java.io.*;4041public class MyOwnSynchronizer {42static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();43static Mutex mutex = new Mutex();44static MyThread thread = new MyThread();45public static void main(String[] argv) throws Exception {46if (!mbean.isSynchronizerUsageSupported()) {47System.out.println("Monitoring of synchronizer usage not supported");48return;49}5051thread.setDaemon(true);52thread.start();5354// wait until myThread acquires mutex55while (!mutex.isLocked()) {56try {57Thread.sleep(100);58} catch (InterruptedException e) {59throw new RuntimeException(e);60}61}6263ThreadDump.threadDump();64// Test dumpAllThreads with locked synchronizers65ThreadInfo[] tinfos = mbean.dumpAllThreads(false, true);66for (ThreadInfo ti : tinfos) {67MonitorInfo[] monitors = ti.getLockedMonitors();68if (monitors.length != 0) {69throw new RuntimeException("Name: " + ti.getThreadName() +70" has non-empty locked monitors = " + monitors.length);71}72LockInfo[] syncs = ti.getLockedSynchronizers();73if (ti.getThreadId() == thread.getId()) {74thread.checkLockedSyncs(ti, syncs);75}76}7778// Test getThreadInfo with locked synchronizers79tinfos = mbean.getThreadInfo(new long[] {thread.getId()}, false, true);80if (tinfos.length != 1) {81throw new RuntimeException("getThreadInfo() returns " +82tinfos.length + " ThreadInfo objects. Expected 0.");83}84ThreadInfo ti = tinfos[0];85if (ti.getLockedMonitors().length != 0) {86throw new RuntimeException("Name: " + ti.getThreadName() +87" has non-empty locked monitors = " +88ti.getLockedMonitors().length);89}90thread.checkLockedSyncs(ti, ti.getLockedSynchronizers());9192System.out.println("Test passed");93}9495static class Mutex implements Lock, java.io.Serializable {9697// Our internal helper class98class Sync extends AbstractQueuedSynchronizer {99// Report whether in locked state100protected boolean isHeldExclusively() {101return getState() == 1;102}103104// Acquire the lock if state is zero105public boolean tryAcquire(int acquires) {106assert acquires == 1; // Otherwise unused107if (compareAndSetState(0, 1)) {108setExclusiveOwnerThread(Thread.currentThread());109return true;110}111return false;112}113114// Release the lock by setting state to zero115protected boolean tryRelease(int releases) {116assert releases == 1; // Otherwise unused117if (getState() == 0) throw new IllegalMonitorStateException();118setExclusiveOwnerThread(null);119setState(0);120return true;121}122123// Provide a Condition124Condition newCondition() { return new ConditionObject(); }125126// Deserialize properly127private void readObject(ObjectInputStream s)128throws IOException, ClassNotFoundException {129s.defaultReadObject();130setState(0); // reset to unlocked state131}132}133134// The sync object does all the hard work. We just forward to it.135private final Sync sync = new Sync();136137public void lock() { sync.acquire(1); }138public boolean tryLock() { return sync.tryAcquire(1); }139public void unlock() { sync.release(1); }140public Condition newCondition() { return sync.newCondition(); }141public boolean isLocked() { return sync.isHeldExclusively(); }142public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }143public void lockInterruptibly() throws InterruptedException {144sync.acquireInterruptibly(1);145}146public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {147return sync.tryAcquireNanos(1, unit.toNanos(timeout));148}149150public AbstractOwnableSynchronizer getSync() { return sync; }151}152153static class MyThread extends Thread {154public MyThread() {155super("MyThread");156}157public void run() {158mutex.lock();159Object o = new Object();160synchronized(o) {161try {162o.wait();163} catch (InterruptedException e) {164throw new RuntimeException(e);165}166}167}168int OWNED_SYNCS = 1;169void checkLockedSyncs(ThreadInfo info, LockInfo[] syncs) {170if (!getName().equals(info.getThreadName())) {171throw new RuntimeException("Name: " + info.getThreadName() +172" not matched. Expected: " + getName());173}174175if (syncs.length != OWNED_SYNCS) {176throw new RuntimeException("Number of locked syncs = " +177syncs.length +178" not matched. Expected: " + OWNED_SYNCS);179}180AbstractOwnableSynchronizer s = mutex.getSync();181String lockName = s.getClass().getName();182int hcode = System.identityHashCode(s);183if (!lockName.equals(syncs[0].getClassName())) {184throw new RuntimeException("LockInfo : " + syncs[0] +185" class name not matched. Expected: " + lockName);186}187if (hcode != syncs[0].getIdentityHashCode()) {188throw new RuntimeException("LockInfo: " + syncs[0] +189" IdentityHashCode not matched. Expected: " + hcode);190}191192}193}194}195196197