|
Java example source code file (JTop.java)
The JTop.java Java example source code/* * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * This source code is provided to illustrate the usage of a given feature * or technique and has been deliberately simplified. Additional steps * required for a production-quality application, such as security checks, * input validation and proper error handling, might not be present in * this sample code. */ /* * * Example of using the java.lang.management API to sort threads * by CPU usage. * * JTop class can be run as a standalone application. * It first establishs a connection to a target VM specified * by the given hostname and port number where the JMX agent * to be connected. It then polls for the thread information * and the CPU consumption of each thread to display every 2 * seconds. * * It is also used by JTopPlugin which is a JConsolePlugin * that can be used with JConsole (see README.txt). The JTop * GUI will be added as a JConsole tab by the JTop plugin. * * @see com.sun.tools.jconsole.JConsolePlugin * * @author Mandy Chung */ import java.lang.management.*; import javax.management.*; import javax.management.remote.*; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.Timer; import java.util.TimerTask; import java.util.TreeMap; import java.util.concurrent.ExecutionException; import java.text.NumberFormat; import java.net.MalformedURLException; import static java.lang.management.ManagementFactory.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.*; /** * JTop is a JPanel to display thread's name, CPU time, and its state * in a table. */ public class JTop extends JPanel { private static class StatusBar extends JPanel { private static final long serialVersionUID = -6483392381797633018L; private final JLabel statusText; public StatusBar(boolean defaultVisible) { super(new GridLayout(1, 1)); statusText = new JLabel(); statusText.setVisible(defaultVisible); add(statusText); } @Override public Dimension getMaximumSize() { Dimension maximum = super.getMaximumSize(); Dimension minimum = getMinimumSize(); return new Dimension(maximum.width, minimum.height); } public void setMessage(String text) { statusText.setText(text); statusText.setVisible(true); } } private static final long serialVersionUID = -1499762160973870696L; private MBeanServerConnection server; private ThreadMXBean tmbean; private MyTableModel tmodel; private final StatusBar statusBar; public JTop() { super(new GridBagLayout()); tmodel = new MyTableModel(); JTable table = new JTable(tmodel); table.setPreferredScrollableViewportSize(new Dimension(500, 300)); // Set the renderer to format Double table.setDefaultRenderer(Double.class, new DoubleRenderer()); // Add some space table.setIntercellSpacing(new Dimension(6,3)); table.setRowHeight(table.getRowHeight() + 4); // Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); // Add the scroll pane to this panel. GridBagConstraints c1 = new GridBagConstraints(); c1.fill = GridBagConstraints.BOTH; c1.gridy = 0; c1.gridx = 0; c1.weightx = 1; c1.weighty = 1; add(scrollPane, c1); statusBar = new StatusBar(false); GridBagConstraints c2 = new GridBagConstraints(); c2.fill = GridBagConstraints.HORIZONTAL; c2.gridy = 1; c2.gridx = 0; c2.weightx = 1.0; c2.weighty = 0.0; add(statusBar, c2); } // Set the MBeanServerConnection object for communicating // with the target VM public void setMBeanServerConnection(MBeanServerConnection mbs) { this.server = mbs; try { this.tmbean = newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, ThreadMXBean.class); } catch (IOException e) { e.printStackTrace(); } if (!tmbean.isThreadCpuTimeSupported()) { statusBar.setMessage("Monitored VM does not support thread CPU time measurement"); } else { try { tmbean.setThreadCpuTimeEnabled(true); } catch (SecurityException e) { statusBar.setMessage("Monitored VM does not have permission for enabling thread cpu time measurement"); } } } class MyTableModel extends AbstractTableModel { private static final long serialVersionUID = -7877310288576779514L; private String[] columnNames = {"ThreadName", "CPU(sec)", "State"}; // List of all threads. The key of each entry is the CPU time // and its value is the ThreadInfo object with no stack trace. private List<Map.Entry Other Java examples (source code examples)Here is a short list of links related to this Java JTop.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.