Skip to content

Commit 02b5e93

Browse files
author
duke
committed
Backport a47f3620f2cae26e2e3f3642bd26871fdd02fddb
1 parent 567ecce commit 02b5e93

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

test/jdk/javax/swing/SwingWorker/TestDoneBeforeDoInBackground.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2026, 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
@@ -23,6 +23,7 @@
2323
/*
2424
* @test
2525
* @bug 8081474
26+
* @library /test/lib
2627
* @summary Verifies if SwingWorker calls 'done'
2728
* before the 'doInBackground' is finished
2829
* @run main TestDoneBeforeDoInBackground
@@ -34,22 +35,25 @@
3435
import java.util.concurrent.TimeUnit;
3536
import java.util.concurrent.atomic.AtomicBoolean;
3637

38+
import jdk.test.lib.Utils;
39+
3740
public class TestDoneBeforeDoInBackground {
3841

39-
private static final int WAIT_TIME = 200;
42+
private static final long WAIT_TIME = Utils.adjustTimeout(200);
4043
private static final long CLEANUP_TIME = 1000;
4144

4245
private static final AtomicBoolean doInBackgroundStarted = new AtomicBoolean(false);
4346
private static final AtomicBoolean doInBackgroundFinished = new AtomicBoolean(false);
4447
private static final AtomicBoolean doneFinished = new AtomicBoolean(false);
4548
private static final CountDownLatch doneLatch = new CountDownLatch(1);
49+
private static final CountDownLatch workerStarted = new CountDownLatch(1);
4650

4751
public static void main(String[] args) throws InterruptedException {
4852
SwingWorker<String, String> worker = new SwingWorker<>() {
4953
@Override
5054
protected String doInBackground() throws Exception {
5155
try {
52-
while (!Thread.currentThread().isInterrupted()) {
56+
while (true) {
5357
System.out.println("Working...");
5458
Thread.sleep(WAIT_TIME);
5559
}
@@ -85,6 +89,12 @@ protected void done() {
8589
worker.addPropertyChangeListener(
8690
new PropertyChangeListener() {
8791
public void propertyChange(PropertyChangeEvent evt) {
92+
if (worker.getState() == SwingWorker.StateValue.STARTED) {
93+
// Now the worker has started and we got a STARTED
94+
// notification. It should be save to cancel now.
95+
workerStarted.countDown();
96+
}
97+
8898
System.out.println("doInBackgroundStarted: " +
8999
doInBackgroundStarted.get() +
90100
" doInBackgroundFinished: " +
@@ -121,7 +131,9 @@ public void propertyChange(PropertyChangeEvent evt) {
121131
}
122132
});
123133
worker.execute();
124-
Thread.sleep(WAIT_TIME * 3);
134+
if (!workerStarted.await(5 * WAIT_TIME, TimeUnit.MILLISECONDS)) {
135+
throw new RuntimeException("worker didn't start in time");
136+
}
125137

126138
final long start = System.currentTimeMillis();
127139
worker.cancel(true);

0 commit comments

Comments
 (0)