From 7c9c382505f7411544add5a68fa326df3b82d2c1 Mon Sep 17 00:00:00 2001 From: valodzka Date: Fri, 22 Jan 2021 12:41:06 +0300 Subject: [PATCH] feat: create heartbeat scheduler with named threads and as daemon (#106) Co-authored-by: Pavel Valodzka --- .../io/socket/engineio/client/Socket.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/socket/engineio/client/Socket.java b/src/main/java/io/socket/engineio/client/Socket.java index 2d86e31a..02de6bd5 100644 --- a/src/main/java/io/socket/engineio/client/Socket.java +++ b/src/main/java/io/socket/engineio/client/Socket.java @@ -4,16 +4,9 @@ import java.net.URI; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -37,6 +30,8 @@ public class Socket extends Emitter { private static final Logger logger = Logger.getLogger(Socket.class.getName()); + private static final AtomicInteger HEARTBEAT_THREAD_COUNTER = new AtomicInteger(); + private static final String PROBE_ERROR = "probe error"; @@ -848,11 +843,22 @@ public String id() { private ScheduledExecutorService getHeartbeatScheduler() { if (this.heartbeatScheduler == null || this.heartbeatScheduler.isShutdown()) { - this.heartbeatScheduler = Executors.newSingleThreadScheduledExecutor(); + this.heartbeatScheduler = createHeartbeatScheduler(); } return this.heartbeatScheduler; } + private ScheduledExecutorService createHeartbeatScheduler() { + return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r, "engine.io-client.heartbeat-" + HEARTBEAT_THREAD_COUNTER.getAndIncrement()); + thread.setDaemon(true); + return thread; + } + }); + } + public static class Options extends Transport.Options { /**