Skip to content

Commit dbb5581

Browse files
committed
8081474: SwingWorker calls 'done' before the 'doInBackground' is finished
Reviewed-by: aivanov, serb
1 parent 306134d commit dbb5581

File tree

2 files changed

+155
-9
lines changed

2 files changed

+155
-9
lines changed

src/java.desktop/share/classes/javax/swing/SwingWorker.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -301,17 +301,16 @@ public SwingWorker() {
301301
new Callable<T>() {
302302
public T call() throws Exception {
303303
setState(StateValue.STARTED);
304-
return doInBackground();
304+
try {
305+
return doInBackground();
306+
} finally {
307+
doneEDT();
308+
setState(StateValue.DONE);
309+
}
305310
}
306311
};
307312

308-
future = new FutureTask<T>(callable) {
309-
@Override
310-
protected void done() {
311-
doneEDT();
312-
setState(StateValue.DONE);
313-
}
314-
};
313+
future = new FutureTask<T>(callable);
315314

316315
state = StateValue.PENDING;
317316
propertyChangeSupport = new SwingWorkerPropertyChangeSupport(this);
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8081474
26+
* @summary Verifies if SwingWorker calls 'done'
27+
* before the 'doInBackground' is finished
28+
* @run main TestDoneBeforeDoInBackground
29+
*/
30+
import java.beans.PropertyChangeListener;
31+
import java.beans.PropertyChangeEvent;
32+
import javax.swing.SwingWorker;
33+
import java.util.concurrent.CountDownLatch;
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.atomic.AtomicBoolean;
36+
37+
public class TestDoneBeforeDoInBackground {
38+
39+
private static final int WAIT_TIME = 200;
40+
private static final long CLEANUP_TIME = 1000;
41+
42+
private static final AtomicBoolean doInBackgroundStarted = new AtomicBoolean(false);
43+
private static final AtomicBoolean doInBackgroundFinished = new AtomicBoolean(false);
44+
private static final AtomicBoolean doneFinished = new AtomicBoolean(false);
45+
private static final CountDownLatch doneLatch = new CountDownLatch(1);
46+
47+
public static void main(String[] args) throws InterruptedException {
48+
SwingWorker<String, String> worker = new SwingWorker<>() {
49+
@Override
50+
protected String doInBackground() throws Exception {
51+
try {
52+
while (!Thread.currentThread().isInterrupted()) {
53+
System.out.println("Working...");
54+
Thread.sleep(WAIT_TIME);
55+
}
56+
} catch (InterruptedException ex) {
57+
System.out.println("Got interrupted!");
58+
}
59+
60+
try {
61+
doInBackgroundStarted.set(true);
62+
System.out.println("Cleaning up");
63+
Thread.sleep(CLEANUP_TIME);
64+
System.out.println("Done cleaning");
65+
doInBackgroundFinished.set(true);
66+
} catch (InterruptedException ex) {
67+
System.out.println("Got interrupted second time!");
68+
}
69+
70+
return null;
71+
}
72+
73+
@Override
74+
protected void done() {
75+
if (!doInBackgroundFinished.get()) {
76+
throw new RuntimeException("done called before " +
77+
"doInBackground is finished");
78+
}
79+
System.out.println("Done");
80+
doneFinished.set(true);
81+
doneLatch.countDown();
82+
}
83+
};
84+
85+
worker.addPropertyChangeListener(
86+
new PropertyChangeListener() {
87+
public void propertyChange(PropertyChangeEvent evt) {
88+
System.out.println("doInBackgroundStarted: " +
89+
doInBackgroundStarted.get() +
90+
" doInBackgroundFinished: " +
91+
doInBackgroundFinished.get() +
92+
" done: " + doneFinished.get() +
93+
" state: " + worker.getState());
94+
// Before doInBackground method is invoked,
95+
// SwingWorker notifies PropertyChangeListeners about the
96+
// property change to StateValue.STARTED
97+
if (doInBackgroundStarted.get()
98+
&& worker.getState() == SwingWorker.StateValue.STARTED) {
99+
throw new RuntimeException(
100+
"PropertyChangeListeners called with " +
101+
"state STARTED after doInBackground is invoked");
102+
}
103+
104+
// Ensure DONE state is notified AFTER
105+
// doInBackground is finished AND done is invoked
106+
if (doInBackgroundFinished.get() && !doneFinished.get()
107+
&& worker.getState() == SwingWorker.StateValue.DONE) {
108+
throw new RuntimeException(
109+
"done method is NOT executed but state is DONE");
110+
}
111+
112+
// After the doInBackground method is finished
113+
// SwingWorker notifies PropertyChangeListeners
114+
// property change to StateValue.DONE
115+
if (worker.getState() != SwingWorker.StateValue.DONE
116+
&& doInBackgroundFinished.get()) {
117+
throw new RuntimeException(
118+
"PropertyChangeListeners called after " +
119+
"doInBackground is finished but before State changed to DONE");
120+
}
121+
}
122+
});
123+
worker.execute();
124+
Thread.sleep(WAIT_TIME * 3);
125+
126+
final long start = System.currentTimeMillis();
127+
worker.cancel(true);
128+
final long end = System.currentTimeMillis();
129+
130+
if ((end - start) > 100) {
131+
throw new RuntimeException("Cancel took too long: "
132+
+ ((end - start) / 1000.0d) + " s");
133+
}
134+
if (!doneLatch.await(CLEANUP_TIME + 1000L, TimeUnit.MILLISECONDS)) {
135+
throw new RuntimeException("done didn't complete in time");
136+
}
137+
System.out.println("doInBackground " + doInBackgroundFinished.get() +
138+
" getState " + worker.getState());
139+
if (worker.getState() != SwingWorker.StateValue.DONE
140+
&& doInBackgroundFinished.get()) {
141+
throw new RuntimeException("doInBackground is finished " +
142+
"but State is not DONE");
143+
}
144+
}
145+
}
146+
147+

0 commit comments

Comments
 (0)