The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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:提供了许多通信和控制功能:
SwingWorker
subclass can define a method, done
, which is automatically invoked on the event dispatch thread when the background task is finished.SwingWorker
子类可以定义一个方法done
,该方法在后台任务完成时在事件分派线程上自动调用。SwingWorker
java.util.concurrent.Future
. java.util.concurrent.Future
。SwingWorker.publish
, causing SwingWorker.process
to be invoked from the event dispatch thread.SwingWorker.publish
来提供中间结果,从而从事件分派线程调用SwingWorker.process
。These features are discussed in the following subsections.这些功能将在以下小节中讨论。
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 在Java教程中,任何提到SwingWorker
now refers to javax.swing.SwingWorker
.SwingWorker
的地方都是指javax.swing.SwingWorker
。