Documentation

The Java™ Tutorials
Hide TOC
Worker Threads and SwingWorker工作线程和SwingWorker
Trail: Creating a GUI With Swing
Lesson: Concurrency in SwingSwing中的并发

Worker Threads and SwingWorkerWorker线程和SwingWorker

When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads. 当Swing程序需要执行长时间运行的任务时,它通常使用一个工作线程,也称为后台线程Each task running on a worker thread is represented by an instance of javax.swing.SwingWorker. 在工作线程上运行的每个任务都由javax.swing.SwingWorker例表示。SwingWorker itself is an abstract class; you must define a subclass in order to create a SwingWorker object; anonymous inner classes are often useful for creating very simple SwingWorker objects.SwingWorker本身是一个抽象类;您必须定义一个子类才能创建SwingWorker对象;匿名内部类通常用于创建非常简单的SwingWorker对象。

SwingWorker provides a number of communication and control features:提供了许多通信和控制功能:

These features are discussed in the following subsections.这些功能将在以下小节中讨论。


Note: 

The javax.swing.SwingWorker class was added to the Java platform in Java SE 6. javax.swing.SwingWorker类被添加到JavaSE6中的Java平台。Prior to this, another class, also called SwingWorker, was widely used for some of the same purposes. 在此之前,另一个类,也称为SwingWorker,被广泛用于一些相同的目的。The old SwingWorker was not part of the Java platform specification, and was not provided as part of the JDK.旧的SwingWorker不是Java平台规范的一部分,也不是作为JDK的一部分提供的。

The new javax.swing.SwingWorker is a completely new class. 新的javax.swing.SwingWorker是一个全新的类。Its functionality is not a strict superset of the old SwingWorker. 它的功能不是旧SwingWorker的严格超集。Methods in the two classes that have the same function do not have the same names. 两个类中具有相同函数的方法的名称不同。Also, instances of the old SwingWorker class were reusable, while a new instance of javax.swing.SwingWorker is needed for each new background task.此外,旧SwingWorker类的实例是可重用的,而每个新的后台任务都需要一个新的javax.swing.SwingWorker实例。

Throughout the Java Tutorials, any mention of SwingWorker now refers to javax.swing.SwingWorker.在Java教程中,任何提到SwingWorker的地方都是指javax.swing.SwingWorker



Previous page: The Event Dispatch Thread
Next page: Simple Background Tasks