private static void checkThreadInfo() throws Exception { // assume all threads stay alive long[] ids = (long[]) server.getAttribute(thread, "AllThreadIds"); Object result = server.invoke(thread, "getThreadInfo", new Object[] {ids}, new String[] {"[J"}); for (CompositeData cd : (CompositeData[]) result) { printThreadInfo(cd); } result = server.invoke( thread, "getThreadInfo", new Object[] {ids, new Integer(2)}, new String[] {"[J", "int"}); for (CompositeData cd : (CompositeData[]) result) { printThreadInfo(cd); } long id = Thread.currentThread().getId(); result = server.invoke(thread, "getThreadInfo", new Object[] {new Long(id)}, new String[] {"long"}); printThreadInfo((CompositeData) result); result = server.invoke( thread, "getThreadInfo", new Object[] {new Long(id), new Integer(2)}, new String[] {"long", "int"}); printThreadInfo((CompositeData) result); }
// Acquire the lock if state is zero public boolean tryAcquire(int acquires) { assert acquires == 1; // Otherwise unused if (compareAndSetState(0, 1)) { setExclusiveOwnerThread(Thread.currentThread()); return true; } return false; }
public static void main(String[] argv) throws Exception { mbean = newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, ThreadMXBean.class); if (!mbean.isSynchronizerUsageSupported()) { System.out.println("Monitoring of synchronizer usage not supported"); return; } thread.setDaemon(true); thread.start(); // wait until myThread acquires mutex and lock owner is set. while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } long[] ids = new long[] {thread.getId()}; // validate the local access ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true); if (infos.length != 1) { throw new RuntimeException( "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1."); } thread.checkThreadInfo(infos[0]); // validate the remote access infos = mbean.getThreadInfo(ids, true, true); if (infos.length != 1) { throw new RuntimeException( "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1."); } thread.checkThreadInfo(infos[0]); boolean found = false; infos = mbean.dumpAllThreads(true, true); for (ThreadInfo ti : infos) { if (ti.getThreadId() == thread.getId()) { thread.checkThreadInfo(ti); found = true; } } if (!found) { throw new RuntimeException("No ThreadInfo found for MyThread"); } System.out.println("Test passed"); }