com.github.phantomthief.pool.impl.KeyAffinityExecutorBuilder Maven / Gradle / Ivy
package com.github.phantomthief.pool.impl;
import static com.github.phantomthief.pool.impl.KeyAffinityExecutorForStats.wrapStats;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator;
import static com.google.common.util.concurrent.MoreExecutors.shutdownAndAwaitTermination;
import static java.util.Collections.unmodifiableCollection;
import static java.util.concurrent.TimeUnit.DAYS;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import com.github.phantomthief.pool.KeyAffinityExecutor;
import com.google.common.util.concurrent.ListeningExecutorService;
/**
* @author w.vela
* Created on 2018-02-09.
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public class KeyAffinityExecutorBuilder {
static final Map, KeyAffinityExecutor>> ALL_EXECUTORS = new ConcurrentHashMap<>();
private final KeyAffinityBuilder builder = new KeyAffinityBuilder<>();
private boolean shutdownAfterClose = true;
@Nonnull
public KeyAffinityExecutor build() {
if (shutdownAfterClose) {
builder.depose(it -> shutdownAndAwaitTermination(it, 1, DAYS));
}
builder.ensure();
KeyAffinityExecutorImpl result = new KeyAffinityExecutorImpl<>(builder::buildInner);
ALL_EXECUTORS.put(result, wrapStats(result));
return result;
}
/**
* @param value default value if {@code true}
*/
@CheckReturnValue
@Nonnull
public KeyAffinityExecutorBuilder shutdownExecutorAfterClose(boolean value) {
shutdownAfterClose = value;
return this;
}
/**
* see {@link KeyAffinityBuilder#usingRandom(boolean)}
*/
@CheckReturnValue
@Nonnull
public KeyAffinityExecutorBuilder usingRandom(boolean value) {
builder.usingRandom(value);
return this;
}
@CheckReturnValue
@Nonnull
public KeyAffinityExecutorBuilder executor(@Nonnull Supplier factory) {
checkNotNull(factory);
builder.factory(() -> {
ExecutorService executor = factory.get();
if (executor instanceof ListeningExecutorService) {
return (ListeningExecutorService) executor;
} else if (executor instanceof ThreadPoolExecutor) {
return new ThreadListeningExecutorService((ThreadPoolExecutor) executor);
} else {
return listeningDecorator(executor);
}
});
return this;
}
@CheckReturnValue
@Nonnull
public KeyAffinityExecutorBuilder count(int count) {
builder.count(count);
return this;
}
public static Collection> getAllExecutors() {
return unmodifiableCollection(ALL_EXECUTORS.values());
}
}