Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions test/jdk/javax/swing/SwingWorker/TestDoneBeforeDoInBackground.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,6 +23,7 @@
/*
* @test
* @bug 8081474
* @library /test/lib
* @summary Verifies if SwingWorker calls 'done'
* before the 'doInBackground' is finished
* @run main TestDoneBeforeDoInBackground
Expand All @@ -34,22 +35,25 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import jdk.test.lib.Utils;

public class TestDoneBeforeDoInBackground {

private static final int WAIT_TIME = 200;
private static final long WAIT_TIME = Utils.adjustTimeout(200);
private static final long CLEANUP_TIME = 1000;

private static final AtomicBoolean doInBackgroundStarted = new AtomicBoolean(false);
private static final AtomicBoolean doInBackgroundFinished = new AtomicBoolean(false);
private static final AtomicBoolean doneFinished = new AtomicBoolean(false);
private static final CountDownLatch doneLatch = new CountDownLatch(1);
private static final CountDownLatch workerStarted = new CountDownLatch(1);

public static void main(String[] args) throws InterruptedException {
SwingWorker<String, String> worker = new SwingWorker<>() {
@Override
protected String doInBackground() throws Exception {
try {
while (!Thread.currentThread().isInterrupted()) {
while (true) {
System.out.println("Working...");
Thread.sleep(WAIT_TIME);
}
Expand Down Expand Up @@ -85,6 +89,12 @@ protected void done() {
worker.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (worker.getState() == SwingWorker.StateValue.STARTED) {
// Now the worker has started and we got a STARTED
// notification. It should be save to cancel now.
workerStarted.countDown();
}

System.out.println("doInBackgroundStarted: " +
doInBackgroundStarted.get() +
" doInBackgroundFinished: " +
Expand Down Expand Up @@ -121,7 +131,9 @@ public void propertyChange(PropertyChangeEvent evt) {
}
});
worker.execute();
Thread.sleep(WAIT_TIME * 3);
if (!workerStarted.await(5 * WAIT_TIME, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("worker didn't start in time");
}

final long start = System.currentTimeMillis();
worker.cancel(true);
Expand Down