void () { List<Integer> disposeLog = new ArrayList<>(); List<Integer> childLog = new ArrayList<>(); for (int i = 0; i < 3; i++) { int taskId = i; myQueue.addTask(new TaskWithEquivalentObject("child") { @Override public void perform(@NotNull ProgressIndicator indicator) { childLog.add(taskId); } @Override public void dispose() { disposeLog.add(taskId); } }); } Assert.assertEquals("No task should run by now " + childLog, Collections.emptyList(), childLog); Assert.assertEquals("older task must be disposed" + disposeLog, Arrays.asList(0, 1), disposeLog); runAllTasks(); Assert.assertEquals("The last task should only run " + childLog, Collections.singletonList(2), childLog); Assert.assertEquals("older task must be disposed" + disposeLog, Arrays.asList(0, 1, 2), disposeLog); }