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:

 * Copyright (c) 2014,, 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 = new JFrame("Frame 2");
      f = new JFrame("Frame 3");
      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 {
         } catch (Exception e) {
         if (t != l[i]) {
            System.out.println("Interrupting thread = "

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
   at java.lang.Object.wait(Native Method)
   at java.lang.ref.ReferenceQueue.remove(
   at java.lang.ref.ReferenceQueue.remove(
AWT blocker activation interrupted:
   at java.lang.Object.wait(Native Method)
   at java.lang.Object.wait(
   at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShut...
   at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.ja...
   at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoSh...
   at Method)

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


 PDF Printing Version