void () { List<Integer> disposeLog = new ArrayList<>(); List<Integer> childLog = new ArrayList<>(); for (int i = 1; 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); } }); myQueue.addTask(new TaskWithEquivalentObject("boss-" + i) { @Override public void perform(@NotNull ProgressIndicator indicator) { childLog.add(-taskId); } @Override public void dispose() { disposeLog.add(-taskId); } }); } runAllTasks(); Assert.assertEquals("The last task should only run " + childLog, Arrays.asList(-1, -2, 3, -3), childLog); Assert.assertEquals("older task must be disposed " + disposeLog, Arrays.asList(1, 2, -1, -2, 3, -3), disposeLog); }