View current Threads in a table : Utilities « Threads « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Threads » UtilitiesScreenshots 
View current Threads in a table
View current Threads in a table
  

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class ThreadViewer extends JPanel {
  private ThreadViewerTableModel tableModel = new ThreadViewerTableModel();

  public ThreadViewer() {

    JTable table = new JTable(tableModel);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

    TableColumnModel colModel = table.getColumnModel();
    int numColumns = colModel.getColumnCount();

    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 void main(String[] args) {
    JFrame f = new JFrame()
    ThreadViewer viewer = new ThreadViewer();

    f.setContentPane(viewer);
    f.setSize(500300);
    f.setVisible(true);
      
    f.setDefaultCloseOperation(1);

    // 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) {
      }
    }
  }
}

class ThreadViewerTableModel extends AbstractTableModel {
  private Object dataLock;

  private int rowCount;

  private Object[][] cellData;

  private Object[][] pendingCellData;

  private final int columnCount;

  private final String[] columnName;

  private final Class[] columnClass;

  private Thread internalThread;

  private volatile boolean noStopRequested;

  public ThreadViewerTableModel() {
    rowCount = 0;
    cellData = new Object[0][0];

    String[] names = "Priority""Alive""Daemon""Interrupted",
        "ThreadGroup""Thread Name" };
    columnName = names;

    Class[] classes = Integer.class, Boolean.class, Boolean.class,
        Boolean.class, String.class, String.class };
    columnClass = classes;

    columnCount = columnName.length;

    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() {
    Runnable transferPending = new Runnable() {
      public void run() {
        transferPendingCellData();
        fireTableDataChanged();
      }
    };

    while (noStopRequested) {
      try {
        createPendingCellData();
        SwingUtilities.invokeAndWait(transferPending);
        Thread.sleep(5000);
      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() {
    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[0new Integer(t.getPriority());
      rowCell[1new Boolean(t.isAlive());
      rowCell[2new Boolean(t.isDaemon());
      rowCell[3new Boolean(t.isInterrupted());
      rowCell[4= t.getThreadGroup().getName();
      rowCell[5= t.getName();
    }

    synchronized (dataLock) {
      pendingCellData = cell;
    }
  }

  private void transferPendingCellData() {
    synchronized (dataLock) {
      cellData = pendingCellData;
      rowCount = cellData.length;
    }
  }

  public int getRowCount() {
    return rowCount;
  }

  public Object getValueAt(int row, int col) {
    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;

    while (group != null) {
      topGroup = group;
      group = group.getParent();
    }

    int estimatedSize = topGroup.activeCount() 2;
    Thread[] slackList = new Thread[estimatedSize];

    int actualSize = topGroup.enumerate(slackList);

    Thread[] list = new Thread[actualSize];
    System.arraycopy(slackList, 0, list, 0, actualSize);

    return list;
  }
}

           
         
    
  
Related examples in the same category
1. Exception call backException call back
2. Listing all threads and threadgroups in the VM.Listing all threads and threadgroups in the VM.
3. Early returnEarly return
4. Transition DetectorTransition Detector
5. Busy Flag
6. Sleep utilities
7. Thread-based logging utility
8. Return a new instance of the given class. Checks the ThreadContext classloader first, then uses the System classloader.
9. Finds a resource with the given name.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.