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发行说明。
It is often useful for a background task to provide interim results while it is still working. 后台任务在工作时提供临时结果通常很有用。The task can do this by invoking 任务可以通过调用SwingWorker.publish
. SwingWorker.publish
来完成此任务。This method accepts a variable number of arguments. 此方法接受数量可变的参数。Each argument must be of the type specified by 每个参数必须是SwingWorker的第二个类型参数指定的类型。SwingWorker
's second type parameter.
To collect results provided by 要收集publish
, override SwingWorker.process
This method will be invoked from the event dispatch thread. publish
提供的结果,请重写SwingWorker.process
。将从事件分派线程调用此方法。Results from multiple invocations of 多次调用publish
are often accumulated for a single invocation of process
.SwingWorker.process
的结果通常会累积为单个process
调用。
Let's look at the way the 让我们看看Flipper.java
example uses publish
to provide interim results. Flipper.java
示例使用publish
提供临时结果的方式。Click the Launch button to run 单击Launch按钮,使用Java™Web启动运行Flipper(下载JDK 7或更高版本)。Flipper
using Java™ Web Start (download JDK 7 or later). Or, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
This program tests the fairness of 该程序通过在后台任务中生成一系列随机java.util.Random
by generating a series of random boolean
values in a background task. boolean
值来测试java.util.Random
的公平性。This is equivalent to flipping a coin; hence the name 这相当于抛硬币;因此命名为Flipper
. Flipper
。To report its results, the background task uses an object of type 要报告结果,后台任务使用FlipPair
FlipPair
类型的对象
private static class FlipPair { private final long heads, total; FlipPair(long heads, long total) { this.heads = heads; this.total = total; } }
The heads
field is the number of times the random value has been true
; the total
field is the total number of random values.heads
字段是随机值为真的次数;总字段是随机值的总数。
The background task is represented by an instance of 后台任务由FlipTask
:FlipTask
的一个实例表示:
private class FlipTask extends SwingWorker<Void, FlipPair> {
Since the task does not return a final result, it does not matter what the first type parameter is; 由于任务不返回最终结果,因此第一个类型参数是什么并不重要;Void
is used as a placeholder. Void
用作占位符。The task invokes 任务在每次“硬币翻转”后调用publish
after each "coin flip":publish
:
@Override protected Void doInBackground() { long heads = 0; long total = 0; Random random = new Random(); while (!isCancelled()) { total++; if (random.nextBoolean()) { heads++; } publish(new FlipPair(heads, total)); } return null; }
(The (下一节将讨论isCancelled
method is discussed in the next section.) Because publish
is invoked very frequently, a lot of FlipPair
values will probably be accumulated before process
is invoked in the event dispatch thread; process
is only interested in the last value reported each time, using it to update the GUI:isCancelled
方法。)由于publish
调用非常频繁,因此在事件分派线程中调用process
之前,可能会积累大量FlipPair
值;process
只对每次报告的最后一个值感兴趣,使用它更新GUI:
protected void process(List<FlipPair> pairs) { FlipPair pair = pairs.get(pairs.size() - 1); headsText.setText(String.format("%d", pair.heads)); totalText.setText(String.format("%d", pair.total)); devText.setText(String.format("%.10g", ((double) pair.heads)/((double) pair.total) - 0.5)); }
If 如果Random
is fair, the value displayed in devText
should get closer and closer to 0 as Flipper
runs.Random
是公平的,那么随着Flipper
的运行,devText
中显示的值应该越来越接近0。
setText
method used in Flipper
is actually "thread safe" as defined in its specification. Flipper
中使用的setText
方法实际上是其规范文档中定义的“线程安全”方法。publish
and process
and set the text fields directly from the worker thread. publish
和process
,直接从工作线程设置文本字段。SwingWorker
interim results. SwingWorker
临时结果的简单演示,我们选择忽略这一事实。