From 42f9d7c193fcd56fda7691b0ea934bae9d23f2d6 Mon Sep 17 00:00:00 2001 From: Sebastian Ortega Date: Mon, 6 Oct 2014 18:52:38 +0200 Subject: [PATCH] Use daemon threads on FullPrunedBlockChain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way, the thread pool used to run the transaction scripts won’t prevent applications from exiting. --- .../org/bitcoinj/core/FullPrunedBlockChain.java | 4 +++- .../org/bitcoinj/net/discovery/DnsDiscovery.java | 3 ++- .../org/bitcoinj/net/discovery/TorDiscovery.java | 5 +++-- .../org/bitcoinj/utils/DaemonThreadFactory.java | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/org/bitcoinj/utils/DaemonThreadFactory.java diff --git a/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java b/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java index 36fc1eca..ebbc3cab 100644 --- a/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java +++ b/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java @@ -21,6 +21,7 @@ import org.bitcoinj.script.Script; import org.bitcoinj.script.Script.VerifyFlag; import org.bitcoinj.store.BlockStoreException; import org.bitcoinj.store.FullPrunedBlockStore; +import org.bitcoinj.utils.DaemonThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -121,7 +122,8 @@ public class FullPrunedBlockChain extends AbstractBlockChain { //TODO: Remove lots of duplicated code in the two connectTransactions // TODO: execute in order of largest transaction (by input count) first - ExecutorService scriptVerificationExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + ExecutorService scriptVerificationExecutor = Executors.newFixedThreadPool( + Runtime.getRuntime().availableProcessors(), new DaemonThreadFactory()); /** A job submitted to the executor which verifies signatures. */ private static class Verifier implements Callable { diff --git a/core/src/main/java/org/bitcoinj/net/discovery/DnsDiscovery.java b/core/src/main/java/org/bitcoinj/net/discovery/DnsDiscovery.java index 28d80b3b..3af85cb4 100644 --- a/core/src/main/java/org/bitcoinj/net/discovery/DnsDiscovery.java +++ b/core/src/main/java/org/bitcoinj/net/discovery/DnsDiscovery.java @@ -19,6 +19,7 @@ package org.bitcoinj.net.discovery; import org.bitcoinj.core.NetworkParameters; import com.google.common.collect.Lists; +import org.bitcoinj.utils.DaemonThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,7 +72,7 @@ public class DnsDiscovery implements PeerDiscovery { // Java doesn't have an async DNS API so we have to do all lookups in a thread pool, as sometimes seeds go // hard down and it takes ages to give up and move on. - ExecutorService threadPool = Executors.newFixedThreadPool(dnsSeeds.length); + ExecutorService threadPool = Executors.newFixedThreadPool(dnsSeeds.length, new DaemonThreadFactory()); try { List> tasks = Lists.newArrayList(); for (final String seed : dnsSeeds) { diff --git a/core/src/main/java/org/bitcoinj/net/discovery/TorDiscovery.java b/core/src/main/java/org/bitcoinj/net/discovery/TorDiscovery.java index 002431ab..ef3037bf 100644 --- a/core/src/main/java/org/bitcoinj/net/discovery/TorDiscovery.java +++ b/core/src/main/java/org/bitcoinj/net/discovery/TorDiscovery.java @@ -34,6 +34,7 @@ import com.subgraph.orchid.circuits.path.CircuitPathChooser; import com.subgraph.orchid.data.HexDigest; import com.subgraph.orchid.data.exitpolicy.ExitTarget; +import org.bitcoinj.utils.DaemonThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -252,8 +253,8 @@ public class TorDiscovery implements PeerDiscovery { } private synchronized void createThreadPool(int size) { - threadPool = - MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(size)); + threadPool = MoreExecutors.listeningDecorator( + Executors.newFixedThreadPool(size, new DaemonThreadFactory())); } private InetAddress lookup(Circuit circuit, String seed) throws UnknownHostException { diff --git a/core/src/main/java/org/bitcoinj/utils/DaemonThreadFactory.java b/core/src/main/java/org/bitcoinj/utils/DaemonThreadFactory.java new file mode 100644 index 00000000..f48d6559 --- /dev/null +++ b/core/src/main/java/org/bitcoinj/utils/DaemonThreadFactory.java @@ -0,0 +1,16 @@ +package org.bitcoinj.utils; + +import javax.annotation.Nonnull; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; + +/** Thread factory whose threads are marked as daemon and won't prevent process exit. */ +public class DaemonThreadFactory implements ThreadFactory { + + @Override + public Thread newThread(@Nonnull Runnable runnable) { + Thread thread = Executors.defaultThreadFactory().newThread(runnable); + thread.setDaemon(true); + return thread; + } +}