Add countdown latch and use while-true loop.

This commit is contained in:
Arno Zeller 2026-06-02 09:44:28 +02:00
parent e83e79460b
commit 4f2420ae1f

View File

@ -43,13 +43,15 @@ public class TestDoneBeforeDoInBackground {
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) {
workerStarted.countDown();
System.out.println("Working...");
Thread.sleep(WAIT_TIME);
}
@ -121,7 +123,7 @@ public class TestDoneBeforeDoInBackground {
}
});
worker.execute();
Thread.sleep(WAIT_TIME * 3);
workerStarted.await();
final long start = System.currentTimeMillis();
worker.cancel(true);