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:
parent
b6eb96cb44
commit
41731c3261
@ -1,7 +1,12 @@
|
|||||||
package com.subgraph.orchid;
|
package com.subgraph.orchid;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.CycleDetectingLockFactory;
|
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;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,4 +47,25 @@ public class Threading {
|
|||||||
public static CycleDetectingLockFactory.Policy getPolicy() {
|
public static CycleDetectingLockFactory.Policy getPolicy() {
|
||||||
return policy;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -20,6 +18,7 @@ import com.subgraph.orchid.Directory;
|
|||||||
import com.subgraph.orchid.ExitCircuit;
|
import com.subgraph.orchid.ExitCircuit;
|
||||||
import com.subgraph.orchid.InternalCircuit;
|
import com.subgraph.orchid.InternalCircuit;
|
||||||
import com.subgraph.orchid.Router;
|
import com.subgraph.orchid.Router;
|
||||||
|
import com.subgraph.orchid.Threading;
|
||||||
import com.subgraph.orchid.TorConfig;
|
import com.subgraph.orchid.TorConfig;
|
||||||
import com.subgraph.orchid.circuits.CircuitManagerImpl.CircuitFilter;
|
import com.subgraph.orchid.circuits.CircuitManagerImpl.CircuitFilter;
|
||||||
import com.subgraph.orchid.circuits.path.CircuitPathChooser;
|
import com.subgraph.orchid.circuits.path.CircuitPathChooser;
|
||||||
@ -53,15 +52,7 @@ public class CircuitCreationTask implements Runnable {
|
|||||||
this.circuitManager = circuitManager;
|
this.circuitManager = circuitManager;
|
||||||
this.initializationTracker = initializationTracker;
|
this.initializationTracker = initializationTracker;
|
||||||
this.pathChooser = pathChooser;
|
this.pathChooser = pathChooser;
|
||||||
this.executor = Executors.newCachedThreadPool(new ThreadFactory() {
|
this.executor = Threading.newPool("CircuitCreationTask worker");
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.setName("CircuitCreationTask worker");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.buildHandler = createCircuitBuildHandler();
|
this.buildHandler = createCircuitBuildHandler();
|
||||||
this.internalBuildHandler = createInternalCircuitBuildHandler();
|
this.internalBuildHandler = createInternalCircuitBuildHandler();
|
||||||
this.predictor = new CircuitPredictor();
|
this.predictor = new CircuitPredictor();
|
||||||
|
@ -60,15 +60,7 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
|
|||||||
private int pendingInternalCircuitCount = 0;
|
private int pendingInternalCircuitCount = 0;
|
||||||
private final TorRandom random;
|
private final TorRandom random;
|
||||||
private final PendingExitStreams pendingExitStreams;
|
private final PendingExitStreams pendingExitStreams;
|
||||||
private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
|
private final ScheduledExecutorService scheduledExecutor = Threading.newSingleThreadScheduledPool("CircuitManager worker");
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.setName("CircuitManager worker");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
private final CircuitCreationTask circuitCreationTask;
|
private final CircuitCreationTask circuitCreationTask;
|
||||||
private final TorInitializationTracker initializationTracker;
|
private final TorInitializationTracker initializationTracker;
|
||||||
private final CircuitPathChooser pathChooser;
|
private final CircuitPathChooser pathChooser;
|
||||||
@ -109,7 +101,6 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
|
|||||||
|
|
||||||
public void stopBuildingCircuits(boolean killCircuits) {
|
public void stopBuildingCircuits(boolean killCircuits) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
isBuilding = false;
|
isBuilding = false;
|
||||||
scheduledExecutor.shutdownNow();
|
scheduledExecutor.shutdownNow();
|
||||||
@ -135,7 +126,6 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!isBuilding) {
|
if (!isBuilding) {
|
||||||
// we were asked to stop since this circuit was started
|
// we were asked to stop since this circuit was started
|
||||||
@ -168,7 +158,6 @@ public class CircuitManagerImpl implements CircuitManager, DashboardRenderable {
|
|||||||
|
|
||||||
int getPendingCircuitCount() {
|
int getPendingCircuitCount() {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return getPendingCircuits().size();
|
return getPendingCircuits().size();
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -6,9 +6,6 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.Executor;
|
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.concurrent.TimeUnit;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -17,6 +14,7 @@ import com.subgraph.orchid.Directory;
|
|||||||
import com.subgraph.orchid.DirectoryDownloader;
|
import com.subgraph.orchid.DirectoryDownloader;
|
||||||
import com.subgraph.orchid.GuardEntry;
|
import com.subgraph.orchid.GuardEntry;
|
||||||
import com.subgraph.orchid.Router;
|
import com.subgraph.orchid.Router;
|
||||||
|
import com.subgraph.orchid.Threading;
|
||||||
import com.subgraph.orchid.TorConfig;
|
import com.subgraph.orchid.TorConfig;
|
||||||
import com.subgraph.orchid.circuits.path.CircuitNodeChooser;
|
import com.subgraph.orchid.circuits.path.CircuitNodeChooser;
|
||||||
import com.subgraph.orchid.circuits.path.CircuitNodeChooser.WeightRule;
|
import com.subgraph.orchid.circuits.path.CircuitNodeChooser.WeightRule;
|
||||||
@ -49,15 +47,7 @@ public class EntryGuards {
|
|||||||
this.pendingProbes = new HashSet<GuardEntry>();
|
this.pendingProbes = new HashSet<GuardEntry>();
|
||||||
this.bridges = new Bridges(config, directoryDownloader);
|
this.bridges = new Bridges(config, directoryDownloader);
|
||||||
this.lock = new Object();
|
this.lock = new Object();
|
||||||
this.executor = Executors.newCachedThreadPool(new ThreadFactory() {
|
this.executor = Threading.newPool("EntryGuards worker");
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.setName("EntryGuards worker");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isUsingBridges() {
|
public boolean isUsingBridges() {
|
||||||
|
@ -7,10 +7,9 @@ import java.net.Socket;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import com.subgraph.orchid.Threading;
|
||||||
import com.subgraph.orchid.data.IPv4Address;
|
import com.subgraph.orchid.data.IPv4Address;
|
||||||
import com.subgraph.orchid.misc.GuardedBy;
|
import com.subgraph.orchid.misc.GuardedBy;
|
||||||
|
|
||||||
@ -38,15 +37,7 @@ public class Dashboard implements DashboardRenderable, DashboardRenderer {
|
|||||||
public Dashboard() {
|
public Dashboard() {
|
||||||
renderables = new CopyOnWriteArrayList<DashboardRenderable>();
|
renderables = new CopyOnWriteArrayList<DashboardRenderable>();
|
||||||
renderables.add(this);
|
renderables.add(this);
|
||||||
executor = Executors.newCachedThreadPool(new ThreadFactory() {
|
executor = Threading.newPool("Dashboard worker");
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.setName("Dashboard worker");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
listeningPort = chooseListeningPort();
|
listeningPort = chooseListeningPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,16 +3,15 @@ package com.subgraph.orchid.directory;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import com.subgraph.orchid.Descriptor;
|
import com.subgraph.orchid.Descriptor;
|
||||||
import com.subgraph.orchid.DirectoryStore;
|
import com.subgraph.orchid.DirectoryStore;
|
||||||
import com.subgraph.orchid.DirectoryStore.CacheFile;
|
import com.subgraph.orchid.DirectoryStore.CacheFile;
|
||||||
|
import com.subgraph.orchid.Threading;
|
||||||
import com.subgraph.orchid.data.HexDigest;
|
import com.subgraph.orchid.data.HexDigest;
|
||||||
import com.subgraph.orchid.directory.parsing.DocumentParser;
|
import com.subgraph.orchid.directory.parsing.DocumentParser;
|
||||||
import com.subgraph.orchid.directory.parsing.DocumentParsingResult;
|
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 DescriptorCacheData<T> data;
|
||||||
|
|
||||||
private final DirectoryStore store;
|
private final DirectoryStore store;
|
||||||
private final ScheduledExecutorService rebuildExecutor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
|
private final ScheduledExecutorService rebuildExecutor =
|
||||||
@Override
|
Threading.newScheduledPool("DescriptorCache rebuild worker");
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.setName("DescriptorCache rebuild worker");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
private final CacheFile cacheFile;
|
private final CacheFile cacheFile;
|
||||||
private final CacheFile journalFile;
|
private final CacheFile journalFile;
|
||||||
|
@ -6,8 +6,6 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -15,6 +13,7 @@ import com.subgraph.orchid.ConsensusDocument;
|
|||||||
import com.subgraph.orchid.Directory;
|
import com.subgraph.orchid.Directory;
|
||||||
import com.subgraph.orchid.DirectoryDownloader;
|
import com.subgraph.orchid.DirectoryDownloader;
|
||||||
import com.subgraph.orchid.KeyCertificate;
|
import com.subgraph.orchid.KeyCertificate;
|
||||||
|
import com.subgraph.orchid.Threading;
|
||||||
import com.subgraph.orchid.TorConfig;
|
import com.subgraph.orchid.TorConfig;
|
||||||
import com.subgraph.orchid.TorConfig.AutoBoolValue;
|
import com.subgraph.orchid.TorConfig.AutoBoolValue;
|
||||||
import com.subgraph.orchid.crypto.TorRandom;
|
import com.subgraph.orchid.crypto.TorRandom;
|
||||||
@ -32,15 +31,7 @@ public class DirectoryDownloadTask implements Runnable {
|
|||||||
private final TorRandom random;
|
private final TorRandom random;
|
||||||
private final DescriptorProcessor descriptorProcessor;
|
private final DescriptorProcessor descriptorProcessor;
|
||||||
|
|
||||||
private final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
|
private final ExecutorService executor = Threading.newPool("DirectoryDownloadTask worker");
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.setName("DirectoryDownloadTask worker");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
private volatile boolean isDownloadingCertificates;
|
private volatile boolean isDownloadingCertificates;
|
||||||
private volatile boolean isDownloadingConsensus;
|
private volatile boolean isDownloadingConsensus;
|
||||||
|
Loading…
Reference in New Issue
Block a user