3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Orchid: refactor thread pool creation

This commit is contained in:
Devrandom 2014-08-27 18:57:39 -07:00 committed by Mike Hearn
parent b6eb96cb44
commit 41731c3261
7 changed files with 38 additions and 68 deletions

View File

@ -1,7 +1,12 @@
package com.subgraph.orchid;
import com.google.common.util.concurrent.CycleDetectingLockFactory;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.locks.ReentrantLock;
/**
@ -42,4 +47,25 @@ public class Threading {
public static CycleDetectingLockFactory.Policy getPolicy() {
return policy;
}
public static ExecutorService newPool(final String name) {
ThreadFactory factory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("name-%d").build();
return Executors.newCachedThreadPool(factory);
}
public static ScheduledExecutorService newSingleThreadScheduledPool(final String name) {
ThreadFactory factory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("name-%d").build();
return Executors.newSingleThreadScheduledExecutor(factory);
}
public static ScheduledExecutorService newScheduledPool(final String name) {
ThreadFactory factory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("name-%d").build();
return Executors.newScheduledThreadPool(1, factory);
}
}

View File

@ -5,8 +5,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -20,6 +18,7 @@ import com.subgraph.orchid.Directory;
import com.subgraph.orchid.ExitCircuit;
import com.subgraph.orchid.InternalCircuit;
import com.subgraph.orchid.Router;
import com.subgraph.orchid.Threading;
import com.subgraph.orchid.TorConfig;
import com.subgraph.orchid.circuits.CircuitManagerImpl.CircuitFilter;
import com.subgraph.orchid.circuits.path.CircuitPathChooser;
@ -53,15 +52,7 @@ public class CircuitCreationTask implements Runnable {
this.circuitManager = circuitManager;
this.initializationTracker = initializationTracker;
this.pathChooser = pathChooser;
this.executor = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("CircuitCreationTask worker");
t.setDaemon(true);
return t;
}
});
this.executor = Threading.newPool("CircuitCreationTask worker");
this.buildHandler = createCircuitBuildHandler();
this.internalBuildHandler = createInternalCircuitBuildHandler();
this.predictor = new CircuitPredictor();

View File

@ -60,15 +60,7 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
private int pendingInternalCircuitCount = 0;
private final TorRandom random;
private final PendingExitStreams pendingExitStreams;
private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("CircuitManager worker");
t.setDaemon(true);
return t;
}
});
private final ScheduledExecutorService scheduledExecutor = Threading.newSingleThreadScheduledPool("CircuitManager worker");
private final CircuitCreationTask circuitCreationTask;
private final TorInitializationTracker initializationTracker;
private final CircuitPathChooser pathChooser;
@ -109,7 +101,6 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
public void stopBuildingCircuits(boolean killCircuits) {
lock.lock();
try {
isBuilding = false;
scheduledExecutor.shutdownNow();
@ -135,7 +126,6 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
}
lock.lock();
try {
if (!isBuilding) {
// we were asked to stop since this circuit was started
@ -168,7 +158,6 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
int getPendingCircuitCount() {
lock.lock();
try {
return getPendingCircuits().size();
} finally {

View File

@ -6,9 +6,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
@ -17,6 +14,7 @@ import com.subgraph.orchid.Directory;
import com.subgraph.orchid.DirectoryDownloader;
import com.subgraph.orchid.GuardEntry;
import com.subgraph.orchid.Router;
import com.subgraph.orchid.Threading;
import com.subgraph.orchid.TorConfig;
import com.subgraph.orchid.circuits.path.CircuitNodeChooser;
import com.subgraph.orchid.circuits.path.CircuitNodeChooser.WeightRule;
@ -49,15 +47,7 @@ public class EntryGuards {
this.pendingProbes = new HashSet<GuardEntry>();
this.bridges = new Bridges(config, directoryDownloader);
this.lock = new Object();
this.executor = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("EntryGuards worker");
t.setDaemon(true);
return t;
}
});
this.executor = Threading.newPool("EntryGuards worker");
}
public boolean isUsingBridges() {

View File

@ -7,10 +7,9 @@ import java.net.Socket;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Logger;
import com.subgraph.orchid.Threading;
import com.subgraph.orchid.data.IPv4Address;
import com.subgraph.orchid.misc.GuardedBy;
@ -38,15 +37,7 @@ public class Dashboard implements DashboardRenderable, DashboardRenderer {
public Dashboard() {
renderables = new CopyOnWriteArrayList<DashboardRenderable>();
renderables.add(this);
executor = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Dashboard worker");
t.setDaemon(true);
return t;
}
});
executor = Threading.newPool("Dashboard worker");
listeningPort = chooseListeningPort();
}

View File

@ -3,16 +3,15 @@ package com.subgraph.orchid.directory;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import com.subgraph.orchid.Descriptor;
import com.subgraph.orchid.DirectoryStore;
import com.subgraph.orchid.DirectoryStore.CacheFile;
import com.subgraph.orchid.Threading;
import com.subgraph.orchid.data.HexDigest;
import com.subgraph.orchid.directory.parsing.DocumentParser;
import com.subgraph.orchid.directory.parsing.DocumentParsingResult;
@ -24,15 +23,8 @@ public abstract class DescriptorCache <T extends Descriptor> {
private final DescriptorCacheData<T> data;
private final DirectoryStore store;
private final ScheduledExecutorService rebuildExecutor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("DescriptorCache rebuild worker");
t.setDaemon(true);
return t;
}
});
private final ScheduledExecutorService rebuildExecutor =
Threading.newScheduledPool("DescriptorCache rebuild worker");
private final CacheFile cacheFile;
private final CacheFile journalFile;

View File

@ -6,8 +6,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
@ -15,6 +13,7 @@ import com.subgraph.orchid.ConsensusDocument;
import com.subgraph.orchid.Directory;
import com.subgraph.orchid.DirectoryDownloader;
import com.subgraph.orchid.KeyCertificate;
import com.subgraph.orchid.Threading;
import com.subgraph.orchid.TorConfig;
import com.subgraph.orchid.TorConfig.AutoBoolValue;
import com.subgraph.orchid.crypto.TorRandom;
@ -32,15 +31,7 @@ public class DirectoryDownloadTask implements Runnable {
private final TorRandom random;
private final DescriptorProcessor descriptorProcessor;
private final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("DirectoryDownloadTask worker");
t.setDaemon(true);
return t;
}
});
private final ExecutorService executor = Threading.newPool("DirectoryDownloadTask worker");
private volatile boolean isDownloadingCertificates;
private volatile boolean isDownloadingConsensus;