Java Swing Tutorials - Herong's Tutorial Examples - Version 4.11, by Dr. Herong Yang
SwingWorker Example using done() Method
This section provides a tutorial example on how to use java.swing.SwingWorker class with the done() method to communicate the calculation result out into a dispatch thread.
There are several ways to use the SwingWorker class. Let's start with the simplest one first.
Example 1: Get notified when task is done and retrieve the result - This example shows the simplest way of using the SwingWorker class to just launch the Worker thread and catch the result in the Dispatch thread. Only 2 methods need to be implemented:
Here is the source code of the example program, SwingWorkerUsingDone.java:
/* SwingWorkerUsingDone.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import javax.swing.SwingWorker; import java.util.List; import java.util.Random; import java.time.LocalTime; public class SwingWorkerUsingDone extends SwingWorker<Integer[], Object> { int total = 100; int wait = 10; // Extending the SwingWorker class protected Integer[] doInBackground() { logMessage("doInBackground() started"); int i = 0; Integer[] l = new Integer[total]; Random r = new Random(); try { while (i<total) { Thread.sleep(wait); Integer n = new Integer(r.nextInt()); l[i] = n; i++; } } catch (Exception e) { e.printStackTrace(); } logMessage("doInBackground() ended"); return l; } protected void done() { logMessage("done() started"); try { Integer[] r = get(); System.out.println("# of element in the result: "+r.length); System.out.println("First element: "+r[0]); System.out.println("Last element: "+r[total-1]); } catch (Exception e) { e.printStackTrace(); } logMessage("done() ended"); } // Launching my extended SwingWorker class public static void main(String[] a) { try { SwingWorker worker = new SwingWorkerUsingDone(); dumpThreads(); worker.execute(); while (true) { dumpThreads(); Thread.sleep(200); } } catch (Exception e) { e.printStackTrace(); } } public static void dumpThreads() { System.out.println(LocalTime.now()+" Thread dump:"); Thread.currentThread().getThreadGroup().list(); } public static void logMessage(String s) { System.out.println(LocalTime.now()+" " +Thread.currentThread().getName()+": "+s); } }
Notes on the sample program:
If you compile and run this example with JDK 1.8, you should get output messages similar to these:
C:\>java SwingWorkerUsingDone 21:59:22.601 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] 21:59:22.683 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] 21:59:22.695 SwingWorker-pool-1-thread-1: doInBackground() started 21:59:22.884 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Shutdown,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] 21:59:23.091 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Shutdown,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] 21:59:23.299 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Shutdown,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] 21:59:23.507 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Shutdown,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] 21:59:23.695 SwingWorker-pool-1-thread-1: doInBackground() ended 21:59:23.714 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Shutdown,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] 21:59:23.730 AWT-EventQueue-0: done() started # of element in the result: 100 First element: 1278389322 Last element: 1363500147 21:59:23.731 AWT-EventQueue-0: done() ended 21:59:23.918 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] 21:59:24.123 Thread dump: java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[SwingWorker-pool-1-thread-1,5,main] Thread[AWT-Windows,6,main] Thread[AWT-EventQueue-0,6,main] ...
Some interesting notes on the output:
The picture below gives you an idea on how the Launching thread, the Worker thread, and the Dispatch thread
worked together in this example where I am only interested to interact with the done() method at the end of
the background task:
Last update: 2014.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
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
JEditorPane - The Editor Pane Class
►SwingWorker - The Background Task Worker
►SwingWorker Example using done() Method
SwingWorker Example using publish() Method
SwingWorker Example using "progress" Property