java查看当前项目所有线程列表界面
java查看当前项目所有线程列表界面
1.TestThread(测试类)
package com.testdemo.pcis.isc.job.king.panel;
public class TestThread {
public static void main(String[] args) {
new Thread(){
public void run() {
try {
Thread.currentThread().sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread(){
public void run() {
try {
Thread.currentThread().sleep(20*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread(){
public void run() {
try {
Thread.currentThread().sleep(30*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
ThreadViewer.showThreads();
}
}
2.ThreadViewer(线程视图启动类)
package com.testdemo.pcis.isc.job.king.panel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class ThreadViewer extends JPanel {
private ThreadViewerTableModel tableModel;
public ThreadViewer() {
tableModel = new ThreadViewerTableModel();
JTable table = new JTable(tableModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
TableColumnModel colModel = table.getColumnModel();
int numColumns = colModel.getColumnCount();
// manually size all but the last column
for ( int i = 0; i < numColumns - 1; i++ ) {
TableColumn col = colModel.getColumn(i);
col.sizeWidthToFit();
col.setPreferredWidth(col.getWidth() + 5);
col.setMaxWidth(col.getWidth() + 5);
}
JScrollPane sp = new JScrollPane(table);
setLayout(new BorderLayout());
add(sp, BorderLayout.CENTER);
}
public void dispose() {
tableModel.stopRequest();
}
protected void finalize() throws Throwable {
dispose();
}
public static JFrame createFramedInstance() {
final ThreadViewer viewer = new ThreadViewer();
final JFrame f = new JFrame("ThreadViewer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.setVisible(false);
f.dispose();
viewer.dispose();
}
});
f.setContentPane(viewer);
f.setSize(500, 300);
f.setVisible(true);
return f;
} public static void showThreads() {
JFrame f = ThreadViewer.createFramedInstance();
// For this example, exit the VM when the viewer
// frame is closed.
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Keep the main thread from exiting by blocking
// on wait() for a notification that never comes.
Object lock = new Object();
synchronized ( lock ) {
try {
lock.wait();
} catch ( InterruptedException x ) {
}
}
} public static void main(String[] args) {
showThreads();
}
}
3.ThreadViewerTableModel(线程视图编织类)
package com.isoftstone.pcis.isc.job.king.panel;
import java.awt.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.table.*;
public class ThreadViewerTableModel extends AbstractTableModel {
private Object dataLock;
private int rowCount;
private Object[][] cellData;
private Object[][] pendingCellData;
// the column information remains constant
private final int columnCount;
private final String[] columnName;
private final Class[] columnClass;
// self-running object control variables
private Thread internalThread;
private volatile boolean noStopRequested;
public ThreadViewerTableModel() {
rowCount = 0;
cellData = new Object[0][0];
// JTable uses this information for the column headers
String[] names = {
"Priority", "Alive",
"Daemon", "Interrupted",
"ThreadGroup", "Thread Name" };
columnName = names; // JTable uses this information for cell rendering
Class[] classes = {
Integer.class, Boolean.class,
Boolean.class, Boolean.class,
String.class, String.class };
columnClass = classes;
columnCount = columnName.length;
// used to control concurrent access
dataLock = new Object();
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try {
runWork();
} catch ( Exception x ) {
// in case ANY exception slips through
x.printStackTrace();
}
}
};
internalThread = new Thread(r, "ThreadViewer");
internalThread.setPriority(Thread.MAX_PRIORITY - 2);
internalThread.setDaemon(true);
internalThread.start();
}
private void runWork() {
// The run() method of transferPending is called by
// the event handling thread for safe concurrency.
Runnable transferPending = new Runnable() {
public void run() {
transferPendingCellData();
// Method of AbstractTableModel that
// causes the table to be updated.
fireTableDataChanged();
}
};
while ( noStopRequested ) {
try {
createPendingCellData();
SwingUtilities.invokeAndWait(transferPending);
Thread.sleep(2000);
} catch ( InvocationTargetException tx ) {
tx.printStackTrace();
stopRequest();
} catch ( InterruptedException x ) {
Thread.currentThread().interrupt();
}
}
}
public void stopRequest() {
noStopRequested = false;
internalThread.interrupt();
}
public boolean isAlive() {
return internalThread.isAlive();
}
private void createPendingCellData() {
// this method is called by the internal thread
Thread[] thread = findAllThreads();
Object[][] cell = new Object[thread.length][columnCount];
for ( int i = 0; i < thread.length; i++ ) {
Thread t = thread[i];
Object[] rowCell = cell[i];
rowCell[0] = new Integer(t.getPriority());
rowCell[1] = new Boolean(t.isAlive());
rowCell[2] = new Boolean(t.isDaemon());
rowCell[3] = new Boolean(t.isInterrupted());
rowCell[4] = t.getThreadGroup().getName();
rowCell[5] = t.getName();
}
synchronized ( dataLock ) {
pendingCellData = cell;
}
}
private void transferPendingCellData() {
// this method is called by the event thread
synchronized ( dataLock ) {
cellData = pendingCellData;
rowCount = cellData.length;
}
}
public int getRowCount() {
// this method is called by the event thread
return rowCount;
} public Object getValueAt(int row, int col) {
// this method is called by the event thread
return cellData[row][col];
}
public int getColumnCount() {
return columnCount;
}
public Class getColumnClass(int columnIdx) {
return columnClass[columnIdx];
}
public String getColumnName(int columnIdx) {
return columnName[columnIdx];
}
public static Thread[] findAllThreads() {
ThreadGroup group =
Thread.currentThread().getThreadGroup();
ThreadGroup topGroup = group;
// traverse the ThreadGroup tree to the top
while ( group != null ) {
topGroup = group;
group = group.getParent();
}
// Create a destination array that is about
// twice as big as needed to be very confident
// that none are clipped.
int estimatedSize = topGroup.activeCount() * 2;
Thread[] slackList = new Thread[estimatedSize];
// Load the thread references into the oversized
// array. The actual number of threads loaded
// is returned.
int actualSize = topGroup.enumerate(slackList);
// copy into a list that is the exact size
Thread[] list = new Thread[actualSize];
System.arraycopy(slackList, 0, list, 0, actualSize);
return list;
}
}
4运行前后比对
运行前界面

运行后界面

java查看当前项目所有线程列表界面的更多相关文章
- java查看当前项目所有线程列表界面【转】
java查看当前项目所有线程列表界面 1.TestThread(测试类) package com.testdemo.pcis.isc.job.king.panel; public class Test ...
- java查看本机hostName可代表的ip列表
java查看本机hostName可代表的ip列表 import java.net.InetAddress; public class ent { public static void main(Str ...
- java查看本机hostName可代表的ip列表【转】
java查看本机hostName可代表的ip列表 import java.net.InetAddress; public class ent { public static void main(Str ...
- Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图(转载)
Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图 2017年04月05日 10:53:13 李学凯 阅读数:104997更多 所属专栏: Intellij Idea ...
- Java程序设计基础项目总结报告
Java程序设计基础项目总结报告 20135313吴子怡 一.项目内容 运用所学Java知识,不调用Java类库,实现密码学相关算法的设计,并完成TDD测试,设计运行界面. 二.具体任务 1.要求实现 ...
- (转)关于java和web项目中的相对路径问题
原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...
- Java概述和项目演示
Java概述和项目演示 1. 软件开发学习方法 多敲 多思考 解决问题 技术文档阅读(中文,英文) 项目文档 多阅读源码 2. 计算机 简称电脑,执行一系列指令的电子设备 3. 硬件组成 输入设备:键 ...
- Java知多少(88)列表和组合框
列表和组合框是又一类供用户选择的界面组件,用于在一组选择项目选择,组合框还可以输入新的选择. 列表 列表(JList)在界面中表现为列表框,是JList类或它的子类的对象.程序可以在列表框中加入多个文 ...
- Java知多少(89)列表和组合框
有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...
随机推荐
- onethink入门笔记(二)
5.onethink页面端获得后台服务器传值的方法 1:一般后台通过assign的值前台通过{$value}显示出来; 2:如果需要在js中使用 则可以通过 在js中写 var m = "{ ...
- MVC执行过程
HttpRuntime中的PR方法1,封装HttpContext2,获取HttpApplication 主要做3件事a,执行本事件时主要调用Init将Global编译得到类型,b,确保Appstart ...
- HandlerThread和IntentService
HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...
- selenium+phantomjs爬取动态页面数据
1.安装selenium pip/pip3 install selenium 注意依赖关系 2.phantomjs for windows 下载地址:http://phantomjs.org/down ...
- 解决sublime3 package control显示There are no packages available for installation
之前一直是在windows上使用sublime,由于公司内部搭建了服务器,干脆把所有项目搬到了服务器上,自然也装上了牛逼闪闪的sublime,然而在接下来安装插件的时候却出了问题,package co ...
- GNS3 桥接虚拟网卡 telnet 实验
网上很多桥接本地网卡的,一直测试不通.无奈,本人桥接vmware 虚拟网卡通! 1: 2: 3:telnet 加密实验 R1(config)#line vt R1(config)#line vty 0 ...
- RunLoop的模式
RunLoop的模式有Default模式.Connection模式.Modal模式.Event tracking模式和Common模式. 1) NSDefaultRunLoopMode: 大多数工作中 ...
- nil与NULL的区别
首先nil表示无值,任何变量在没有被赋值之前的值都为nil,对于真假判断,只有nil与false表示假,其余均为真.而NULL是一个宏定义,值为0.并且,nil一般赋值给空对象,NULL一般赋值给ni ...
- tableView中cell的重用机制
如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象,然而OS设备的内存是有限的,这样就将耗尽iOS设备的内存.要解决这个问题,需要提到重用UITable ...
- js的原型模式
以下内容来自<JavaScript高级程序设计>第三版 我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个指针,指向一个对象,而这个对象的用途是包含可以由特定类型的所 ...