< prev index next >

test/jdk/java/lang/management/ManagementFactory/ThreadMXBeanProxy.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2016, 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 
  24 /*
  25  * @test
  26  * @bug     5086470 6358247 7193302
  27  * @summary Test type conversion when invoking ThreadMXBean.dumpAllThreads
  28  *          through proxy.
  29  * @author  Mandy Chung
  30  *
  31  * @run main ThreadMXBeanProxy
  32  */
  33 
  34 import static java.lang.management.ManagementFactory.*;
  35 import java.lang.management.*;
  36 import java.util.*;
  37 import java.util.concurrent.locks.*;
  38 import java.util.concurrent.TimeUnit;
  39 import java.io.*;
  40 import javax.management.*;
  41 
  42 public class ThreadMXBeanProxy {
  43     private static MBeanServer server =
  44         ManagementFactory.getPlatformMBeanServer();
  45     private static ThreadMXBean mbean;
  46     static Mutex mutex = new Mutex();
  47     static Object lock = new Object();

  48     static MyThread thread = new MyThread();
  49     public static void main(String[] argv) throws Exception {
  50         mbean = newPlatformMXBeanProxy(server,
  51                                        THREAD_MXBEAN_NAME,
  52                                        ThreadMXBean.class);
  53 
  54         if (!mbean.isSynchronizerUsageSupported()) {
  55             System.out.println("Monitoring of synchronizer usage not supported");
  56             return;
  57         }
  58 
  59         thread.setDaemon(true);
  60         thread.start();
  61 
  62         // wait until myThread acquires mutex and lock owner is set.
  63         while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) {
  64            try {
  65                Thread.sleep(100);
  66            } catch (InterruptedException e) {
  67                throw new RuntimeException(e);
  68            }
  69         }
  70 






  71         long[] ids = new long[] { thread.getId() };
  72 
  73         // validate the local access
  74         ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true);
  75         if (infos.length != 1) {
  76             throw new RuntimeException("Returned ThreadInfo[] of length=" +
  77                 infos.length + ". Expected to be 1.");
  78         }
  79         thread.checkThreadInfo(infos[0]);
  80 
  81         // validate the remote access
  82         infos = mbean.getThreadInfo(ids, true, true);
  83         if (infos.length != 1) {
  84             throw new RuntimeException("Returned ThreadInfo[] of length=" +
  85                 infos.length + ". Expected to be 1.");
  86         }
  87         thread.checkThreadInfo(infos[0]);
  88 
  89         boolean found = false;
  90         infos = mbean.dumpAllThreads(true, true);
  91         for (ThreadInfo ti : infos) {
  92             if (ti.getThreadId() == thread.getId()) {
  93                 thread.checkThreadInfo(ti);
  94                 found = true;
  95             }
  96         }
  97 
  98         if (!found) {
  99             throw new RuntimeException("No ThreadInfo found for MyThread");
 100         }
 101 
 102         System.out.println("Test passed");
 103     }
 104 
 105     static class MyThread extends Thread {
 106         public MyThread() {
 107             super("MyThread");
 108         }
 109         public void run() {
 110             synchronized (lock) {

 111                 mutex.lock();
 112                 Object o = new Object();
 113                 synchronized(o) {
 114                     try {
 115                         o.wait();
 116                     } catch (InterruptedException e) {
 117                         throw new RuntimeException(e);
 118                     }
 119                 }
 120             }
 121         }
 122 
 123         int OWNED_MONITORS = 1;
 124         int OWNED_SYNCS = 1;
 125         void checkThreadInfo(ThreadInfo info) {
 126             if (!getName().equals(info.getThreadName())) {
 127                 throw new RuntimeException("Name: " + info.getThreadName() +
 128                     " not matched. Expected: " + getName());
 129             }
 130 
 131             MonitorInfo[] monitors = info.getLockedMonitors();
 132             if (monitors.length != OWNED_MONITORS) {
 133                 throw new RuntimeException("Number of locked monitors = " +
 134                     monitors.length +
 135                     " not matched. Expected: " + OWNED_MONITORS);


   1 /*
   2  * Copyright (c) 2005, 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 
  24 /*
  25  * @test
  26  * @bug     5086470 6358247 7193302 8048215
  27  * @summary Test type conversion when invoking ThreadMXBean.dumpAllThreads
  28  *          through proxy.
  29  * @author  Mandy Chung
  30  *
  31  * @run main ThreadMXBeanProxy
  32  */
  33 
  34 import static java.lang.management.ManagementFactory.*;
  35 import java.lang.management.*;
  36 import java.util.*;
  37 import java.util.concurrent.locks.*;
  38 import java.util.concurrent.TimeUnit;
  39 import java.io.*;
  40 import javax.management.*;
  41 
  42 public class ThreadMXBeanProxy {
  43     private static MBeanServer server =
  44         ManagementFactory.getPlatformMBeanServer();
  45     private static ThreadMXBean mbean;
  46     static Mutex mutex = new Mutex();
  47     static Object lock = new Object();
  48     static Object waiter = new Object();
  49     static MyThread thread = new MyThread();
  50     public static void main(String[] argv) throws Exception {
  51         mbean = newPlatformMXBeanProxy(server,
  52                                        THREAD_MXBEAN_NAME,
  53                                        ThreadMXBean.class);
  54 
  55         if (!mbean.isSynchronizerUsageSupported()) {
  56             System.out.println("Monitoring of synchronizer usage not supported");
  57             return;
  58         }
  59 
  60         thread.setDaemon(true);
  61         thread.start();
  62 
  63         // wait until myThread acquires mutex and lock owner is set.
  64         while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) {
  65            try {
  66                Thread.sleep(100);
  67            } catch (InterruptedException e) {
  68                throw new RuntimeException(e);
  69            }
  70         }
  71 
  72         // 'thread' holds the mutex, which means it must also have the monitor of
  73         // 'waiter' at least until it does the wait(). So we acquire the monitor of
  74         // 'waiter' here, which ensures that 'thread' must be in wait()
  75         synchronized(waiter) {
  76         }
  77 
  78         long[] ids = new long[] { thread.getId() };
  79 
  80         // validate the local access
  81         ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true);
  82         if (infos.length != 1) {
  83             throw new RuntimeException("Returned ThreadInfo[] of length=" +
  84                 infos.length + ". Expected to be 1.");
  85         }
  86         thread.checkThreadInfo(infos[0]);
  87 
  88         // validate the remote access
  89         infos = mbean.getThreadInfo(ids, true, true);
  90         if (infos.length != 1) {
  91             throw new RuntimeException("Returned ThreadInfo[] of length=" +
  92                 infos.length + ". Expected to be 1.");
  93         }
  94         thread.checkThreadInfo(infos[0]);
  95 
  96         boolean found = false;
  97         infos = mbean.dumpAllThreads(true, true);
  98         for (ThreadInfo ti : infos) {
  99             if (ti.getThreadId() == thread.getId()) {
 100                 thread.checkThreadInfo(ti);
 101                 found = true;
 102             }
 103         }
 104 
 105         if (!found) {
 106             throw new RuntimeException("No ThreadInfo found for MyThread");
 107         }
 108 
 109         System.out.println("Test passed");
 110     }
 111 
 112     static class MyThread extends Thread {
 113         public MyThread() {
 114             super("MyThread");
 115         }
 116         public void run() {
 117             synchronized (lock) {
 118                 synchronized(waiter) {
 119                     mutex.lock();


 120                     try {
 121                         waiter.wait();
 122                     } catch (InterruptedException e) {
 123                         throw new RuntimeException(e);
 124                     }
 125                 }
 126             }
 127         }
 128 
 129         int OWNED_MONITORS = 1;
 130         int OWNED_SYNCS = 1;
 131         void checkThreadInfo(ThreadInfo info) {
 132             if (!getName().equals(info.getThreadName())) {
 133                 throw new RuntimeException("Name: " + info.getThreadName() +
 134                     " not matched. Expected: " + getName());
 135             }
 136 
 137             MonitorInfo[] monitors = info.getLockedMonitors();
 138             if (monitors.length != OWNED_MONITORS) {
 139                 throw new RuntimeException("Number of locked monitors = " +
 140                     monitors.length +
 141                     " not matched. Expected: " + OWNED_MONITORS);


< prev index next >