Re: calling SwingWorker cancel in WindowClosing event handler
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 31 Mar 2007 09:57:09 -0700
yancheng.cheok@xxxxxxxxx wrote:
hello,
i realize that when after i call SwingWorker cancel and try to wait
until the task is really canceled (through SwingWorker.get), i will
get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html
Note: If get is invoked on a SwingWorker object after its background
task has been cancelled, java.util.concurrent.CancellationException is
thrown.
Hence, i was wondering after the CancellationException caught, when
the time the next statement is executed, does the task is really
cancelled?
thanks
private void formWindowClosing(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
try
{
marketTask.cancel(false);
marketTask.get();
}
catch(InterruptedException exp) {
}
catch(java.util.concurrent.ExecutionException exp)
{
}
catch(java.util.concurrent.CancellationException exp)
{
}
// So, does market Task really cancelled???
}
You will know that marketTask is canceled by checking the return value of cancel(). Calling get() will not block. It will just throw a CancellationException.
From the docs:
cancel
public final boolean cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.
--
Knute Johnson
email s/nospam/knute/
.
- References:
- calling SwingWorker cancel in WindowClosing event handler
- From: yancheng . cheok
- calling SwingWorker cancel in WindowClosing event handler
- Prev by Date: Re: calling SwingWorker cancel in WindowClosing event handler
- Previous by thread: Re: calling SwingWorker cancel in WindowClosing event handler
- Next by thread: calling SwingWorker cancel in WindowClosing event handler
- Index(es):
Relevant Pages
|