Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
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) 1997-2024 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 with JDK 1.5 or older on a Windows computer, you will see 3 frame windows showing up on the screen. After every 30 seconds, the 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 the 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 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)
This program behaves differently on JDK 1.6 or newer versions as shown in next tutorials.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
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 20
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
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
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
AWT (Abstract Windows Toolkit)