From 3844c89c1c3a7051241cc92b9caf934633148d16 Mon Sep 17 00:00:00 2001 From: Bill Kuker Date: Thu, 14 Apr 2011 12:46:24 +0000 Subject: [PATCH 1/1] Add a debug window, and a thread lister --- .../rocketry/motorsim/debug/DebugFrame.java | 18 +++++ .../rocketry/motorsim/debug/ThreadsPanel.java | 65 +++++++++++++++++++ .../visual/workbench/MotorWorkbench.java | 12 ++++ 3 files changed, 95 insertions(+) create mode 100644 gui/com/billkuker/rocketry/motorsim/debug/DebugFrame.java create mode 100644 gui/com/billkuker/rocketry/motorsim/debug/ThreadsPanel.java diff --git a/gui/com/billkuker/rocketry/motorsim/debug/DebugFrame.java b/gui/com/billkuker/rocketry/motorsim/debug/DebugFrame.java new file mode 100644 index 0000000..025f621 --- /dev/null +++ b/gui/com/billkuker/rocketry/motorsim/debug/DebugFrame.java @@ -0,0 +1,18 @@ +package com.billkuker.rocketry.motorsim.debug; + +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; + +public class DebugFrame extends JFrame { + private static final long serialVersionUID = 1L; + + public DebugFrame(){ + setSize(800,600); + setTitle("MotorSim - Debug"); + JTabbedPane tabs = new JTabbedPane(); + this.setContentPane(tabs); + tabs.add("Threads", new JScrollPane(new ThreadsPanel())); + this.setVisible(true); + } +} diff --git a/gui/com/billkuker/rocketry/motorsim/debug/ThreadsPanel.java b/gui/com/billkuker/rocketry/motorsim/debug/ThreadsPanel.java new file mode 100644 index 0000000..81818c5 --- /dev/null +++ b/gui/com/billkuker/rocketry/motorsim/debug/ThreadsPanel.java @@ -0,0 +1,65 @@ +package com.billkuker.rocketry.motorsim.debug; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; + +import javax.swing.JTable; +import javax.swing.SwingUtilities; +import javax.swing.table.DefaultTableModel; + +public class ThreadsPanel extends JTable { + private static final long serialVersionUID = 1L; + + private DefaultTableModel tm = new DefaultTableModel(); + static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean(); + + public ThreadsPanel() { + tm.addColumn("Name"); + tm.addColumn("ID"); + tm.addColumn("CPU"); + setModel(tm); + + new Thread() { + { + setDaemon(true); + setName("Debug - Thread List"); + } + @Override + public void run() { + while (true) { + try { + SwingUtilities.invokeLater(new Thread() { + @Override + public void run() { + try { + while (tm.getRowCount() > 0) + tm.removeRow(0); + + long[] tids = tmbean.getAllThreadIds(); + ThreadInfo[] tinfos = tmbean.getThreadInfo( + tids, Integer.MAX_VALUE); + for (ThreadInfo ti : tinfos) { + tm.addRow(new Object[] { + ti.getThreadName(), + ti.getThreadId(), + tmbean.getThreadCpuTime(ti + .getThreadId()), + }); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + }); + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }.start(); + } + +} diff --git a/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorWorkbench.java b/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorWorkbench.java index 79e979a..a22583d 100644 --- a/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorWorkbench.java +++ b/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorWorkbench.java @@ -17,6 +17,7 @@ import javax.swing.JRadioButtonMenuItem; import javax.swing.JSeparator; import com.billkuker.rocketry.motorsim.RocketScience.UnitPreference; +import com.billkuker.rocketry.motorsim.debug.DebugFrame; import com.billkuker.rocketry.motorsim.fuel.FuelsEditor; import com.billkuker.rocketry.motorsim.visual.RememberJFrame; @@ -229,6 +230,17 @@ public class MotorWorkbench extends RememberJFrame { }); } }); + add(new JMenuItem("Debug") { + private static final long serialVersionUID = 1L; + { + addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + new DebugFrame(); + } + }); + } + }); } }); } -- 2.30.2