OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / management / TestThread.java
1 /* TestThread.java -- Tests the thread bean.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21 package gnu.classpath.examples.management;
22
23 import java.lang.management.ManagementFactory;
24 import java.lang.management.ThreadInfo;
25 import java.lang.management.ThreadMXBean;
26
27 import java.util.Arrays;
28
29 public class TestThread
30 {
31
32   public static void main(String[] args)
33   {
34     ThreadMXBean bean = ManagementFactory.getThreadMXBean();
35     System.out.println("Bean: " + bean);
36     System.out.println("Monitor deadlocked threads: " + bean.findMonitorDeadlockedThreads());
37     long[] ids = bean.getAllThreadIds();
38     System.out.println("Live thread ids: " + Arrays.toString(ids));
39     boolean currentTimeMonitoring = bean.isCurrentThreadCpuTimeSupported();
40     System.out.println("Current thread CPU time monitoring supported: " + currentTimeMonitoring);
41     if (currentTimeMonitoring)
42       {
43         boolean timeEnabled = bean.isThreadCpuTimeEnabled();
44         System.out.println("Is time monitoring enabled... " + 
45                            (timeEnabled ? "yes" : "no"));
46         if (!timeEnabled)
47           {
48             System.out.println("Enabling...");
49             bean.setThreadCpuTimeEnabled(true);
50             timeEnabled = bean.isThreadCpuTimeEnabled();
51             System.out.println("Should now be enabled... " +
52                            (timeEnabled ? "yes" : "no"));
53           }
54         if (timeEnabled)
55           {
56             System.out.println("Current thread CPU time: " 
57                                + bean.getCurrentThreadCpuTime()
58                                + "ns");
59             System.out.println("Current thread user time: " 
60                                + bean.getCurrentThreadUserTime()
61                                + "ns");
62           }
63       }
64     System.out.println("Daemon thread count: " + bean.getDaemonThreadCount());
65     System.out.println("Peak thread count: " + bean.getPeakThreadCount());
66     System.out.println("Resetting...");
67     bean.resetPeakThreadCount();
68     System.out.println("Peak thread count: " + bean.getPeakThreadCount());
69     System.out.println("Thread count: " + bean.getThreadCount());
70     boolean timeMonitoring = bean.isThreadCpuTimeSupported();
71     System.out.println("Thread CPU time monitoring supported: " + timeMonitoring);
72     if (timeMonitoring)
73       {
74         for (int a = 0; a < ids.length; ++a)
75           {
76             System.out.println("Thread " + a 
77                                + " CPU time: " 
78                                + bean.getThreadCpuTime(ids[a]) + "ns");
79             System.out.println("Thread " 
80                                + a + " user time: " 
81                                + bean.getThreadUserTime(ids[a]) + "ns");
82           }
83       }
84     System.out.println("Current thread info: "
85                        + bean.getThreadInfo(Thread.currentThread().getId()));
86     System.out.println("All thread info: " + Arrays.toString(bean.getThreadInfo(ids)));
87     System.out.println("Total started threads: " + bean.getTotalStartedThreadCount());
88     boolean contentionMonitoring = bean.isThreadContentionMonitoringSupported();
89     System.out.println("Thread contention monitoring supported: " + contentionMonitoring);
90     if (contentionMonitoring)
91       {
92         boolean contentionEnabled = bean.isThreadContentionMonitoringEnabled();
93         System.out.println("Thread contention monitoring shouldn't be enabled... " + 
94                            (contentionEnabled ? "but it is" : "true"));
95         if (!contentionEnabled)
96           {
97             System.out.println("Enabling...");
98             bean.setThreadContentionMonitoringEnabled(true);
99             contentionEnabled = bean.isThreadContentionMonitoringEnabled();
100             System.out.println("Should now be enabled... " +
101                                (contentionEnabled ? "it is" : "nope"));
102           }
103         if (contentionEnabled)
104           {
105             ThreadInfo[] info = bean.getThreadInfo(ids);
106             for (int a = 0; a < info.length; ++a)
107               {
108                 System.out.println("Blocked time for thread " 
109                                    + info[a].getThreadId() + ": " 
110                                    + info[a].getBlockedTime() + "ms");
111                 System.out.println("Waited time for thread " 
112                                    + info[a].getThreadId() + ": " 
113                                    + info[a].getWaitedTime() + "ms");
114               }
115           }
116       }
117   }
118 }