On Tue, 31 Jan 2023 23:53:18 GMT, Sergey Bylokhov <s...@openjdk.org> wrote:

> How the updated code is supposed to work if the worker will have some state 
> updated by the "doInBackground" and used by the "done" method.

What do you mean?

~~Not much has changed to the way `done` is called.~~ Previously `doneEDT` was 
called from `FutureTask` implementation, its overridden `done` method. Now, 
`doneEDT` is called right from `doInBackground`.

Thus, if `cancel` was called on EDT, `doneEDT` and consequently 
`SwingWorker.done` were called on EDT (the latter was called synchronously). 
That is `doneEDT` was called on the thread which called `cancel`.

Now, `doneEDT` is always called on the background thread whether it's cancelled 
or not.

Yet no one should have depended on that because it was an implementation detail 
rather than a contract.

> I guess if the "doInBackground" will be canceled the "done" method may throw 
> an exception?

Why is it? `done` cannot throw exceptions.

This entire scenario is where *`doInBackground` is cancelled* but `done` gets 
called before `doInBackground` exits.

Yet `get` method throws `CancellationException` if the work for 
`doInBackground` is cancelled. It's specified in the [`Future` 
interface](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html#get()).

> Do we have such usage in our codebase?

I found the 
[`CheckCancellationException.java`](https://github.com/openjdk/jdk/blob/master/test/jdk/javax/swing/SwingWorker/6608234/CheckCancellationException.java)
 test that you wrote. It verifies that calling `get` throws 
`CancellationException`.

`SwingWorker` is used in `JEditorPane` as well as in `jconsole` packages. Yet I 
didn't look into the details how it's used.

-------------

PR: https://git.openjdk.org/jdk/pull/11940

Reply via email to