博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java查看当前项目所有线程列表界面【转】
阅读量:4660 次
发布时间:2019-06-09

本文共 9301 字,大约阅读时间需要 31 分钟。

 

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;      }  }
View Code

 

 

4运行前后比对

运行前界面

 

 

运行后界面

 

 

 

转载于:https://www.cnblogs.com/whatlonelytear/p/5653788.html

你可能感兴趣的文章
DRF 版本 认证
查看>>
MVC 4将jQuery升级到1.9出现各种问题。。。
查看>>
JedisPool资源池优化
查看>>
第2次作业+105032014140
查看>>
JS防抖与节流
查看>>
redhat enterprixe 5.0 DNS 服务配置与管理
查看>>
Django ORM不完全操作
查看>>
hdu2089
查看>>
windows下使用PHP实现定时执行脚本
查看>>
Linux下常用的压缩与解压命令
查看>>
第一周的博客作业
查看>>
linux的awk命令解读
查看>>
JavaScript内部原理实践——真的懂JavaScript吗?(转)
查看>>
android5.0之toolBar
查看>>
POJ--2689-C++
查看>>
better-scroll的使用方法,动态创建dom使用better-scroll
查看>>
PHP中的面向对象魔术方法大全
查看>>
数据库常用函数详解
查看>>
jquery 监听不起效果的小问题汇总
查看>>
eclipse构建及运行maven web项目
查看>>