Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
To cancel a running background task, invoke
SwingWorker.cancel
The task must cooperate with its own cancellation. There are two ways it can do this:
SwingWorker.isCancelled
at short intervals. This method returns true
if cancel
has been invoked for this SwingWorker
.The cancel
method takes a single boolean
argument. If the argument is true
, cancel
sends the background task an interrupt. Whether the argument is true
or false
, invoking cancel
changes the cancellation status of the object to true
. This is the value returned by isCancelled
. Once changed, the cancellation status cannot be changed back.
The Flipper
example from the previous section uses the status-only idiom. The main loop in doInBackground
exits when isCancelled
returns true
. This will occur when the user clicks the "Cancel" button, triggering code that invokes cancel
with an argument of false
.
The status-only approach makes sense for Flipper
because its implementation of SwingWorker.doInBackground
does not include any code that might throw InterruptedException
. To respond to an interrupt, the background task would have to invoke Thread.isInterrupted
at short intervals. It's just as easy to use SwingWorker.isCancelled
for the same purpose
get
is invoked on a SwingWorker
object after its background task has been cancelled, java.util.concurrent.CancellationException
is thrown.