com.github.phantomthief.pool.impl.KeyAffinityExecutorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of more-lambdas Show documentation
Show all versions of more-lambdas Show documentation
Some useful lambda implements for Java 8.
The newest version!
package com.github.phantomthief.pool.impl;
import static com.github.phantomthief.pool.impl.KeyAffinityExecutorBuilder.ALL_EXECUTORS;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.util.concurrent.Futures.addCallback;
import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.github.phantomthief.pool.KeyAffinityExecutor;
import com.github.phantomthief.pool.KeyAffinityExecutorStats;
import com.github.phantomthief.pool.KeyAffinityExecutorStats.SingleThreadPoolStats;
import com.github.phantomthief.util.ThrowableRunnable;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.UncheckedExecutionException;
/**
* @author w.vela
* Created on 2018-11-30.
*/
class KeyAffinityExecutorImpl extends LazyKeyAffinity implements
KeyAffinityExecutor {
private ConcurrentMap> substituentTaskMap;
private boolean skipDuplicate = false;
KeyAffinityExecutorImpl(Supplier> factory) {
super(factory);
}
void setSkipDuplicate(boolean skipDuplicate) {
this.skipDuplicate = skipDuplicate;
if (skipDuplicate && substituentTaskMap == null) {
substituentTaskMap = new ConcurrentHashMap<>();
}
}
@Override
public void close() throws Exception {
try {
super.close();
} finally {
ALL_EXECUTORS.remove(this);
}
}
@Nullable
@Override
public KeyAffinityExecutorStats stats() {
List list = new ArrayList<>();
for (ListeningExecutorService executor : this) {
if (executor instanceof ThreadListeningExecutorService) {
ThreadListeningExecutorService t1 = (ThreadListeningExecutorService) executor;
list.add(new SingleThreadPoolStats(t1.getMaximumPoolSize(), t1.getActiveCount(),
t1.getQueueSize(), t1.getQueueRemainingCapacity()));
} else {
throw new IllegalStateException("cannot get stats for " + this);
}
}
return new KeyAffinityExecutorStats(list);
}
@Override
public ListenableFuture submit(K key, @Nonnull Callable task) {
checkNotNull(task);
if (skipDuplicate) {
task = wrapSkipCheck(key, task);
if (task == null) {
return immediateCancelledFuture();
}
}
ListeningExecutorService service = select(key);
boolean addCallback = false;
try {
ListenableFuture future = service.submit(task);
addCallback(future, new FutureCallback © 2015 - 2025 Weber Informatics LLC | Privacy Policy