RFR: 8081474: SwingWorker calls 'done' before the 'doInBackground' is finished [v10]

Alexey Ivanov aivanov at openjdk.org
Fri Feb 3 13:10:59 UTC 2023


On Fri, 3 Feb 2023 12:31:08 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

>>> It seems the order of sequence is listener->State.STARTED->doInBackground->listener->DONE->done
>> 
>> Yes, but the different order is specified: listener(STARTED) -> doInBackground -> done -> listener(DONE).
>
> But then it will violate
> https://github.com/openjdk/jdk/blob/810c8a271b4524ae776e2306ef699e04a7d145a2/src/java.desktop/share/classes/javax/swing/SwingWorker.java#L288-L293

Hm… not really, state will transition to `DONE` after `doInBackground` is finished.

In fact, it'll happen soon enough: `doneEDT` will schedule execution of `done` on EDT and exit. Then the state will change to `DONE`. Yet listeners are notified on EDT, to this notification will also be scheduled to run on EDT.

Very much likely that the state is `DONE` when `done` is executed but it's not guaranteed.

---

The specification isn't clear, and some parts somewhat contradict each other. Logically, the state should transition to `DONE` as soon as `doInBackground` completes. Then both `done` method and listeners are called when the state is `DONE` that is *the background work is complete*.

Yet as it's specified now, the `done` method is called while the state is still `STARTED`. This kind of implies calling `get` may block because the result of computation may not be available yet. However, it's not the case: `get` never blocks if called from `done`: it either returns the result or throws `CancellationException`.

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

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



More information about the client-libs-dev mailing list