Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.concurrent.Executor;

import static java.lang.ScopedValue.where;
import static java.util.Objects.requireNonNull;

/**
Expand All @@ -26,7 +27,7 @@
public class ReentrantBoundedExecutor
implements Executor
{
private final ThreadLocal<Boolean> executorThreadMarkers = ThreadLocal.withInitial(() -> false);
private final ScopedValue<Void> executorThreadMarkers = ScopedValue.newInstance();
private final Executor boundedExecutor;
private final Executor coreExecutor;

Expand All @@ -39,20 +40,13 @@ public ReentrantBoundedExecutor(Executor coreExecutor, int maxThreads)
@Override
public void execute(Runnable task)
{
if (executorThreadMarkers.get()) {
// We are just interested whether this is reentrant execution
if (executorThreadMarkers.isBound()) {
// schedule recursive task immediately as it's being scheduled from currently executed task
coreExecutor.execute(task);
return;
}

boundedExecutor.execute(() -> {
executorThreadMarkers.set(true);
try {
task.run();
}
finally {
executorThreadMarkers.remove();
}
});
boundedExecutor.execute(() -> where(executorThreadMarkers, null).run(task));
}
}