void () { CyclicBarrier b = new CyclicBarrier(2); AtomicReference<Boolean> isRun = new AtomicReference<>(); myQueue.addTask(new TaskWithEquivalentObject("any") { @Override public void perform(@NotNull ProgressIndicator indicator) { for (int i = 0; i < 2; i++) { await(b); } isRun.set(indicator.isCanceled()); } }); Thread th = new Thread(() -> { try (MergingTaskQueue.QueuedTask<?> nextTask = myQueue.extractNextTask()) { nextTask.executeTask(); } catch (Exception e) { LOG.error(e); } }, getClass().getName() + "-thread"); th.setDaemon(true); th.start(); await(b); //now the task is in the middle myQueue.disposePendingTasks(); await(b); //not it should complete try { th.join(5_000); } catch (InterruptedException e) { th.interrupt(); Assert.fail(); } Assert.assertEquals(Boolean.TRUE, isRun.get()); }