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) 1997-2018 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 with JDK 1.5 or older, 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:

herong> \Progra~1\Java\jdk1.5.0_09\bin\java FrameThreads

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)

Table of Contents

 About This Book

 JDK (Java Development Kit)

 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 in JDK 1.6

 JFrame Thread Behavior with JDK 8 to 12

 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

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box 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

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Archived Tutorials


 Full Version in PDF/EPUB