Listing and Interrupting AWT Threads

This section provides a tutorial example on how to list AWT threads and how to interrupt AWT threads with the interrupt() method.

Problem: I want to know how many threads are created by the Swing package and the AWT package. And what will happen if I send the interrupt signal to all of them.

Solution: This is easy. Just get the list of all threads of the current thread group, and interrupt them one by one. Here is a sample program to show you how to do this:

/* FrameThreads.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.awt.*;
import javax.swing.*;
public class FrameThreads {
   public static void main(String[] a) {
      JFrame f = new JFrame("Frame 1");
      f.setBounds(0,0,100,100);
      f.setVisible(true);
      f = new JFrame("Frame 2");
      f.setBounds(50,50,100,100);
      f.setVisible(true);
      f = new JFrame("Frame 3");
      f.setBounds(100,100,100,100);
      f.setVisible(true);
      Thread[] l = new Thread[100];
      int n = Thread.enumerate(l);
      Thread g = null;
      for (int i=0; i<n; i++) {
         System.out.println("Active thread = "+l[i].getName());
      }
      Thread t = Thread.currentThread();
      for (int i=0; i<n; i++) {
         try {
            Thread.sleep(1000*30);
         } catch (Exception e) {
            System.out.println("Interrupted.");
         }
         if (t != l[i]) {
            System.out.println("Interrupting thread = "
               +l[i].getName());
            l[i].interrupt();
         }
      }
   }
}

If you run this program, you will see 3 frame windows showing up on the screen. After every 30 seconds, my program will send an interrupt signal to each thread. When the interrupt signal reaches the "Java2D Disposer" thread, a run-time exception occurs forcing the application to terminate. The following list shows you the output of my program:

Active thread = main
Active thread = AWT-Windows
Active thread = AWT-Shutdown
Active thread = AWT-EventQueue-0
Active thread = Java2D Disposer
Interrupting thread = AWT-Windows
Interrupting thread = AWT-Shutdown
Interrupting thread = AWT-EventQueue-0
Interrupting thread = Java2D Disposer
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
   at java.lang.Object.wait(Native Method)
   at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:111)
   at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:127)
   at sun.java2d.Disposer.run(Disposer.java:100)
   at java.lang.Thread.run(Thread.java:534)
AWT blocker activation interrupted:
java.lang.InterruptedException
   at java.lang.Object.wait(Native Method)
   at java.lang.Object.wait(Object.java:429)
   at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShut...
   at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.ja...
   at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoSh...
   at sun.awt.windows.WToolkit.eventLoop(Native Method)
   at sun.awt.windows.WToolkit.run(WToolkit.java:262)
   at java.lang.Thread.run(Thread.java:534)

Note 1: Swing (AWT) system uses 4 threads: AWT-Windows, AWT-Shutdown, AWT-EventQueue-0, and Java2D Disposer.

Note 2: When thread "Java2D Disposer" is interrupted, the entire application terminates.

Sample programs listed in this section have been tested with JDK 1.3.1 to 1.5.0.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

JFrame - Main Frame Class

 Creating Frames with Sizes and Locations

 Closing Frame and Terminating Application

Listing and Interrupting AWT Threads

 "AWT blocker activation interrupted" Error

 "AWT blocker activation interrupted" Error in JDK 1.7

 Displaying Chinese Characters in Frame Title

 Drawing Graphics - Using paint() on Frame

 Drawing Graphics - Using paint() on Component

 Drawing Graphics - Using paint() on Content Pane

 Drawing Chinese Characters on Frames

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 References

 PDF Printing Version