diff --git a/core/src/main/java/bisq/core/app/AvoidStandbyModeService.java b/core/src/main/java/bisq/core/app/AvoidStandbyModeService.java index bf9a733718e..48f7c8f4a6a 100644 --- a/core/src/main/java/bisq/core/app/AvoidStandbyModeService.java +++ b/core/src/main/java/bisq/core/app/AvoidStandbyModeService.java @@ -70,9 +70,7 @@ public void init() { private void start() { isStopped = false; log.info("AvoidStandbyModeService started"); - Thread thread = new Thread(this::play); - thread.setName("AvoidStandbyModeService-thread"); - thread.start(); + new Thread(this::play, "AvoidStandbyModeService-thread").start(); } diff --git a/core/src/main/java/bisq/core/app/BisqSetup.java b/core/src/main/java/bisq/core/app/BisqSetup.java index 44e0d15245f..cd4d2f4e1ac 100644 --- a/core/src/main/java/bisq/core/app/BisqSetup.java +++ b/core/src/main/java/bisq/core/app/BisqSetup.java @@ -434,11 +434,8 @@ private void checkIfLocalHostNodeIsRunning() { bisqEnvironment.getIgnoreLocalBtcNode()) { step3(); } else { - Thread checkIfLocalHostNodeIsRunningThread = new Thread(() -> { - Thread.currentThread().setName("checkIfLocalHostNodeIsRunningThread"); - Socket socket = null; - try { - socket = new Socket(); + new Thread(() -> { + try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), BisqEnvironment.getBaseCurrencyNetwork().getParameters().getPort()), 5000); log.info("Localhost Bitcoin node detected."); @@ -448,16 +445,8 @@ private void checkIfLocalHostNodeIsRunning() { }); } catch (Throwable e) { UserThread.execute(BisqSetup.this::step3); - } finally { - if (socket != null) { - try { - socket.close(); - } catch (IOException ignore) { - } - } } - }); - checkIfLocalHostNodeIsRunningThread.start(); + }, "checkIfLocalHostNodeIsRunningThread").start(); } } @@ -474,9 +463,8 @@ private void checkCryptoSetup() { // If users compile themselves they might miss that step and then would get an exception in the trade. // To avoid that we add here at startup a sample encryption and signing to see if it don't causes an exception. // See: https://github.com/bisq-network/exchange/blob/master/doc/build.md#7-enable-unlimited-strength-for-cryptographic-keys - Thread checkCryptoThread = new Thread(() -> { + new Thread(() -> { try { - Thread.currentThread().setName("checkCryptoThread"); // just use any simple dummy msg Ping payload = new Ping(1, 1); SealedAndSigned sealedAndSigned = EncryptionService.encryptHybridWithSignature(payload, @@ -496,8 +484,7 @@ private void checkCryptoSetup() { if (cryptoSetupFailedHandler != null) cryptoSetupFailedHandler.accept(msg); } - }); - checkCryptoThread.start(); + }, "checkCryptoThread").start(); } private void startP2pNetworkAndWallet() { diff --git a/core/src/main/java/bisq/core/app/SetupUtils.java b/core/src/main/java/bisq/core/app/SetupUtils.java index f3bfcd9fccd..0edf297d2c7 100644 --- a/core/src/main/java/bisq/core/app/SetupUtils.java +++ b/core/src/main/java/bisq/core/app/SetupUtils.java @@ -49,40 +49,35 @@ public static void checkCryptoSetup(KeyRing keyRing, EncryptionService encryptio // If users compile themselves they might miss that step and then would get an exception in the trade. // To avoid that we add here at startup a sample encryption and signing to see if it don't causes an exception. // See: https://github.com/bisq-network/exchange/blob/master/doc/build.md#7-enable-unlimited-strength-for-cryptographic-keys - Thread checkCryptoThread = new Thread() { - @Override - public void run() { - try { - Thread.currentThread().setName("checkCryptoThread"); - // just use any simple dummy msg - Ping payload = new Ping(1, 1); - SealedAndSigned sealedAndSigned = EncryptionService.encryptHybridWithSignature(payload, - keyRing.getSignatureKeyPair(), keyRing.getPubKeyRing().getEncryptionPubKey()); - DecryptedDataTuple tuple = encryptionService.decryptHybridWithSignature(sealedAndSigned, - keyRing.getEncryptionKeyPair().getPrivate()); - if (tuple.getNetworkEnvelope() instanceof Ping && - ((Ping) tuple.getNetworkEnvelope()).getNonce() == payload.getNonce() && - ((Ping) tuple.getNetworkEnvelope()).getLastRoundTripTime() == payload.getLastRoundTripTime()) { - log.debug("Crypto test succeeded"); + Thread checkCryptoThread = new Thread(() -> { + try { + // just use any simple dummy msg + Ping payload = new Ping(1, 1); + SealedAndSigned sealedAndSigned = EncryptionService.encryptHybridWithSignature(payload, + keyRing.getSignatureKeyPair(), keyRing.getPubKeyRing().getEncryptionPubKey()); + DecryptedDataTuple tuple = encryptionService.decryptHybridWithSignature(sealedAndSigned, + keyRing.getEncryptionKeyPair().getPrivate()); + if (tuple.getNetworkEnvelope() instanceof Ping && + ((Ping) tuple.getNetworkEnvelope()).getNonce() == payload.getNonce() && + ((Ping) tuple.getNetworkEnvelope()).getLastRoundTripTime() == payload.getLastRoundTripTime()) { + log.debug("Crypto test succeeded"); - UserThread.execute(resultHandler::handleResult); - } else { - errorHandler.accept(new CryptoException("Payload not correct after decryption")); - } - } catch (CryptoException | ProtobufferException e) { - log.error(e.toString()); - e.printStackTrace(); - errorHandler.accept(e); + UserThread.execute(resultHandler::handleResult); + } else { + errorHandler.accept(new CryptoException("Payload not correct after decryption")); } + } catch (CryptoException | ProtobufferException e) { + log.error(e.toString()); + e.printStackTrace(); + errorHandler.accept(e); } - }; + }, "checkCryptoThread"); checkCryptoThread.start(); } public static BooleanProperty readFromResources(P2PDataStorage p2PDataStorage) { BooleanProperty result = new SimpleBooleanProperty(); - Thread thread = new Thread(() -> { - Thread.currentThread().setName("readFromResourcesThread"); + new Thread(() -> { // Used to load different files per base currency (EntryMap_BTC_MAINNET, EntryMap_LTC,...) final BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork(); final String postFix = "_" + baseCurrencyNetwork.name(); @@ -90,8 +85,7 @@ public static BooleanProperty readFromResources(P2PDataStorage p2PDataStorage) { p2PDataStorage.readFromResources(postFix); log.info("readFromResources took {} ms", (new Date().getTime() - ts)); UserThread.execute(() -> result.set(true)); - }); - thread.start(); + }, "readFromResourcesThread").start(); return result; } } diff --git a/core/src/main/java/bisq/core/btc/setup/WalletConfig.java b/core/src/main/java/bisq/core/btc/setup/WalletConfig.java index 95a441b258c..0a74eef93ed 100644 --- a/core/src/main/java/bisq/core/btc/setup/WalletConfig.java +++ b/core/src/main/java/bisq/core/btc/setup/WalletConfig.java @@ -561,13 +561,12 @@ private Wallet createWallet(BisqKeyChainGroup keyChainGroup, boolean isBsqWallet private void installShutdownHook() { if (autoStop) Runtime.getRuntime().addShutdownHook(new Thread(() -> { - Thread.currentThread().setName("ShutdownHook"); try { WalletConfig.this.stopAsync(); WalletConfig.this.awaitTerminated(); } catch (Throwable ignore) { } - })); + }, "WalletConfig ShutdownHook")); } @Override diff --git a/desktop/src/main/java/bisq/desktop/main/overlays/windows/downloadupdate/BisqInstaller.java b/desktop/src/main/java/bisq/desktop/main/overlays/windows/downloadupdate/BisqInstaller.java index b23c1c9d93d..c342995258c 100644 --- a/desktop/src/main/java/bisq/desktop/main/overlays/windows/downloadupdate/BisqInstaller.java +++ b/desktop/src/main/java/bisq/desktop/main/overlays/windows/downloadupdate/BisqInstaller.java @@ -94,8 +94,7 @@ public Optional download(String version) { public VerifyTask verify(List fileDescriptors) { VerifyTask verifyTask = new VerifyTask(fileDescriptors); - Thread th = new Thread(verifyTask); - th.start(); + new Thread(verifyTask, "BisqInstaller VerifyTask").start(); // TODO: check for problems when creating task return verifyTask; } @@ -120,8 +119,7 @@ private static DownloadTask downloadFiles(List fileDescriptors, if (saveDir == null) saveDir = Utilities.getDownloadOfHomeDir(); DownloadTask task = new DownloadTask(fileDescriptors, saveDir); - Thread th = new Thread(task); - th.start(); + new Thread(task, "BisqInstaller DownloadTask").start(); // TODO: check for problems when creating task return task; } diff --git a/monitor/src/main/java/bisq/monitor/Monitor.java b/monitor/src/main/java/bisq/monitor/Monitor.java index da454b0a2ff..5e7b8d19bc9 100644 --- a/monitor/src/main/java/bisq/monitor/Monitor.java +++ b/monitor/src/main/java/bisq/monitor/Monitor.java @@ -21,11 +21,11 @@ import bisq.monitor.metric.P2PMarketStats; import bisq.monitor.metric.P2PNetworkLoad; import bisq.monitor.metric.P2PRoundTripTime; +import bisq.monitor.metric.P2PSeedNodeSnapshot; import bisq.monitor.metric.PriceNodeStats; import bisq.monitor.metric.TorHiddenServiceStartupTime; import bisq.monitor.metric.TorRoundTripTime; import bisq.monitor.metric.TorStartupTime; -import bisq.monitor.metric.P2PSeedNodeSnapshot; import bisq.monitor.reporter.ConsoleReporter; import bisq.monitor.reporter.GraphiteReporter; @@ -114,33 +114,29 @@ private void start() throws Throwable { configure(); // exit Metrics gracefully on shutdown - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - // set the name of the Thread for debugging purposes - setName("shutdownHook"); - - log.info("system shutdown initiated"); - - log.info("shutting down active metrics..."); - Metric.haltAllMetrics(); - - try { - log.info("shutting down tor..."); - Tor tor = Tor.getDefault(); - checkNotNull(tor, "tor must not be null"); - tor.shutdown(); - } catch (Throwable ignore) { - } - - log.info("system halt"); - } - }); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + // set the name of the Thread for debugging purposes + log.info("system shutdown initiated"); + + log.info("shutting down active metrics..."); + Metric.haltAllMetrics(); + + try { + log.info("shutting down tor..."); + Tor tor = Tor.getDefault(); + checkNotNull(tor, "tor must not be null"); + tor.shutdown(); + } catch (Throwable ignore) { + } + + log.info("system halt"); + }, "Monitor Shutdown Hook ") + ); } /** * Reload the configuration from disk. - * + * * @throws Exception if something goes wrong */ private void configure() throws Exception { diff --git a/p2p/src/main/java/bisq/network/p2p/network/Connection.java b/p2p/src/main/java/bisq/network/p2p/network/Connection.java index 3f543f7f466..989dc9729bf 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/Connection.java +++ b/p2p/src/main/java/bisq/network/p2p/network/Connection.java @@ -140,7 +140,7 @@ public static int getPermittedMessageSize() { private final ConnectionListener connectionListener; @Getter private final String uid; - private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); + private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "Connection.java executor-service")); // holder of state shared between InputHandler and Connection @Getter private final Statistic statistic; @@ -489,7 +489,6 @@ public void shutDown(CloseConnectionReason closeConnectionReason, @Nullable Runn if (closeConnectionReason.sendCloseMessage) { new Thread(() -> { - Thread.currentThread().setName("Connection:SendCloseConnectionMessage-" + this.uid); try { String reason = closeConnectionReason == CloseConnectionReason.RULE_VIOLATION ? getRuleViolation().name() : closeConnectionReason.name(); @@ -506,7 +505,7 @@ public void shutDown(CloseConnectionReason closeConnectionReason, @Nullable Runn stopped = true; UserThread.execute(() -> doShutDown(closeConnectionReason, shutDownCompleteHandler)); } - }).start(); + }, "Connection:SendCloseConnectionMessage-" + this.uid).start(); } else { stopped = true; doShutDown(closeConnectionReason, shutDownCompleteHandler);