Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #349 DelegatingScheduler: Bill Pugh Singleton Implementation #350

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bug (delay == 0); fix bad commonPool() as executor; use less memory: …
…don't capture variable with commonPool
magicprinc committed Nov 26, 2022
commit 95627ff290d53cc71e48002f52c63f0329708adf
Original file line number Diff line number Diff line change
@@ -50,8 +50,11 @@ private DelegatingScheduler() {
this.executorService = null;
}

public DelegatingScheduler(ExecutorService executor) {
this.executorService = executor;
public DelegatingScheduler(ExecutorService executor){
if (executor != ForkJoinPool.commonPool() || useCommonPool())
this.executorService = executor;
else // don't use commonPool(): cannot support parallelism @see CompletableFuture#useCommonPool
this.executorService = null;
}

private static final class LazyDelayerHolder {
@@ -75,14 +78,17 @@ private static final class LazyForkJoinPoolHolder {
private static final ForkJoinPool FORK_JOIN_POOL = create();

private static ForkJoinPool create(){
return ForkJoinPool.getCommonPoolParallelism() > 1
? ForkJoinPool.commonPool()
return useCommonPool() ? ForkJoinPool.commonPool()
: new ForkJoinPool(Math.max(Runtime.getRuntime().availableProcessors(), 2),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
}

static boolean useCommonPool () {
return ForkJoinPool.getCommonPoolParallelism() > 1;
}

static final class ScheduledCompletableFuture<V> extends CompletableFuture<V> implements ScheduledFuture<V> {
// Guarded by this
volatile Future<V> delegate;
@@ -159,12 +165,22 @@ public ScheduledFuture<?> schedule(Callable<?> callable, long delay, TimeUnit un
return null;
};

if (delay == 0)
if (delay <= 0)
promise.delegate = es.submit(completingCallable);

// use less memory: don't capture variable with commonPool
else if (es == LazyForkJoinPoolHolder.FORK_JOIN_POOL)
promise.delegate = delayer().schedule(()->{
// Guard against race with promise.cancel
synchronized(promise) {
if (!promise.isCancelled())
promise.delegate = LazyForkJoinPoolHolder.FORK_JOIN_POOL.submit(completingCallable);
}
}, delay, unit);
else
promise.delegate = delayer().schedule(() -> {
promise.delegate = delayer().schedule(()->{
// Guard against race with promise.cancel
synchronized (promise) {
synchronized(promise) {
if (!promise.isCancelled())
promise.delegate = es.submit(completingCallable);
}