|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 | /* |
24 | 24 | * @test |
25 | 25 | * @bug 8081474 |
| 26 | + * @library /test/lib |
26 | 27 | * @summary Verifies if SwingWorker calls 'done' |
27 | 28 | * before the 'doInBackground' is finished |
28 | 29 | * @run main TestDoneBeforeDoInBackground |
|
34 | 35 | import java.util.concurrent.TimeUnit; |
35 | 36 | import java.util.concurrent.atomic.AtomicBoolean; |
36 | 37 |
|
| 38 | +import jdk.test.lib.Utils; |
| 39 | + |
37 | 40 | public class TestDoneBeforeDoInBackground { |
38 | 41 |
|
39 | | - private static final int WAIT_TIME = 200; |
| 42 | + private static final long WAIT_TIME = Utils.adjustTimeout(200); |
40 | 43 | private static final long CLEANUP_TIME = 1000; |
41 | 44 |
|
42 | 45 | private static final AtomicBoolean doInBackgroundStarted = new AtomicBoolean(false); |
43 | 46 | private static final AtomicBoolean doInBackgroundFinished = new AtomicBoolean(false); |
44 | 47 | private static final AtomicBoolean doneFinished = new AtomicBoolean(false); |
45 | 48 | private static final CountDownLatch doneLatch = new CountDownLatch(1); |
| 49 | + private static final CountDownLatch workerStarted = new CountDownLatch(1); |
46 | 50 |
|
47 | 51 | public static void main(String[] args) throws InterruptedException { |
48 | 52 | SwingWorker<String, String> worker = new SwingWorker<>() { |
49 | 53 | @Override |
50 | 54 | protected String doInBackground() throws Exception { |
51 | 55 | try { |
52 | | - while (!Thread.currentThread().isInterrupted()) { |
| 56 | + while (true) { |
53 | 57 | System.out.println("Working..."); |
54 | 58 | Thread.sleep(WAIT_TIME); |
55 | 59 | } |
@@ -85,6 +89,12 @@ protected void done() { |
85 | 89 | worker.addPropertyChangeListener( |
86 | 90 | new PropertyChangeListener() { |
87 | 91 | 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 | + |
88 | 98 | System.out.println("doInBackgroundStarted: " + |
89 | 99 | doInBackgroundStarted.get() + |
90 | 100 | " doInBackgroundFinished: " + |
@@ -121,7 +131,9 @@ public void propertyChange(PropertyChangeEvent evt) { |
121 | 131 | } |
122 | 132 | }); |
123 | 133 | 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 | + } |
125 | 137 |
|
126 | 138 | final long start = System.currentTimeMillis(); |
127 | 139 | worker.cancel(true); |
|
0 commit comments