RFR: 8081474: SwingWorker calls 'done' before the 'doInBackground' is finished [v12]
Alexey Ivanov
aivanov at openjdk.org
Fri Feb 3 13:21:55 UTC 2023
On Fri, 3 Feb 2023 07:27:39 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:
>> SwingWorker done() method [spec ](https://github.com/openjdk/jdk/blob/master/src/java.desktop/share/classes/javax/swing/SwingWorker.java#L452) says "Executed on the Event Dispatch Thread after the doInBackground method is finished"
>> but there's no mechanism in place to honor that claim.
>> The [spec](https://github.com/openjdk/jdk/blob/master/src/java.desktop/share/classes/javax/swing/SwingWorker.java#L289)
>> also says the state should be DONE after doInBackground() returns which is also not done.
>>
>> Modified the code to honour the specification.
>
> Prasanta Sadhukhan has updated the pull request incrementally with one additional commit since the last revision:
>
> jcheck fix
The more I think about it, the more I'm inclined towards this change may be unnecessary at all. Yet the spec needs to be updated.
Consider the following scenario. The user starts a long running operation, so `doInBackground` is being executed. It may block access to UI and show the progress. Then they decide they don't to wait any more, so they cancel the operation by clicking *Cancel* button the UI which calls `cancel(true)` on `SwingWorker`.
The thread that runs `doInBackground` is interrupted. It may not exit yet but *does it matter?*
>From the user's point of view, the operation is already cancelled. That fact that the background thread needs to perform some clean-up actions is irrelevant.
>From the developer's point of view, it does matter. It depends on what actions are valid on this instance of `SwingWorker`. Until `doInBackground` finishes, a resource may still be busy while it performs its clean-up.
Can the clean-up be run in background too? There's no univocal answer to this question: in some cases, it can; in other cases, it cannot.
Thus, with the current approach in this fix, `SwingWorker` provides stronger guarantees which are stated in the spec that `done` is called only after `doInBackground` finishes whether it's cancelled or not.
Before this fix, the guarantee was that the call to `get` doesn't block when `done` is called or when the listeners are notified about the state `DONE`.
-------------
PR: https://git.openjdk.org/jdk/pull/11940
More information about the client-libs-dev
mailing list