void () { ApplicationManager.getApplication().invokeAndWait(() -> { AtomicBoolean executed = new AtomicBoolean(); Runnable testRunnable = () -> { executed.set(true); }; AtomicBoolean gotPCE = new AtomicBoolean(); LaterInvocator.enterModal(myWindow1); var indicator = new EmptyProgressIndicator(ModalityState.nonModal()); ApplicationManager.getApplication().executeOnPooledThread(() -> { ProgressManager.getInstance().runProcess(() -> { try { // this should schedule the runnable with NON_MODAL ApplicationManager.getApplication().invokeAndWait(testRunnable); } catch (ProcessCanceledException pce) { gotPCE.set(true); throw pce; } }, indicator); }); LockSupport.parkNanos(50_000_000); // give chance to schedule testRunnable assertReferenced(LaterInvocator.class, testRunnable); // ensure testRunnable in the queue UIUtil.dispatchAllInvocationEvents(); assertReferenced(LaterInvocator.class, testRunnable); // ensure testRunnable is still in the queue indicator.cancel(); LockSupport.parkNanos(50_000_000); // give chance to loop and call checkCanceled assertFalse(executed.get()); assertTrue(gotPCE.get()); LeakHunter.checkLeak(LaterInvocator.class, testRunnable.getClass()); }); }