diff --git a/junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/ForkJoinPoolHierarchicalTestExecutorService.java b/junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/ForkJoinPoolHierarchicalTestExecutorService.java index 9b5d2efea14c..4b2e332fd07a 100644 --- a/junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/ForkJoinPoolHierarchicalTestExecutorService.java +++ b/junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/ForkJoinPoolHierarchicalTestExecutorService.java @@ -253,17 +253,17 @@ public boolean exec() { // this means that .join() will wait. return false; } - try (ResourceLock lock = resourceLock.acquire()) { - threadLock.incrementNesting(lock); + try ( // + ResourceLock lock = resourceLock.acquire(); // + @SuppressWarnings("unused") + ThreadLock.NestedResourceLock nested = threadLock.withNesting(lock) // + ) { testTask.execute(); return true; } catch (InterruptedException e) { throw ExceptionUtils.throwAsUncheckedException(e); } - finally { - threadLock.decrementNesting(); - } } @Override @@ -300,18 +300,19 @@ void addDeferredTask(ExclusiveTask task) { deferredTasks.add(task); } - void incrementNesting(ResourceLock lock) { + NestedResourceLock withNesting(ResourceLock lock) { locks.push(lock); - } - - @SuppressWarnings("resource") - void decrementNesting() { - locks.pop(); + return locks::pop; } boolean areAllHeldLocksCompatibleWith(ResourceLock lock) { return locks.stream().allMatch(l -> l.isCompatible(lock)); } + + interface NestedResourceLock extends AutoCloseable { + @Override + void close(); + } } interface TaskEventListener {