diff --git a/src/main/java/com/jcraft/jsch/ChannelDirectTCPIP.java b/src/main/java/com/jcraft/jsch/ChannelDirectTCPIP.java index c1522efb..075772c1 100644 --- a/src/main/java/com/jcraft/jsch/ChannelDirectTCPIP.java +++ b/src/main/java/com/jcraft/jsch/ChannelDirectTCPIP.java @@ -63,7 +63,7 @@ public void connect(int connectTimeout) throws JSchException { } if (io.in != null) { - thread = new Thread(this::run); + thread = _session.getThreadFactory().newThread(this::run); thread.setName("DirectTCPIP thread " + _session.getHost()); if (_session.daemon_thread) { thread.setDaemon(_session.daemon_thread); diff --git a/src/main/java/com/jcraft/jsch/ChannelExec.java b/src/main/java/com/jcraft/jsch/ChannelExec.java index 56143bbb..8feeb349 100644 --- a/src/main/java/com/jcraft/jsch/ChannelExec.java +++ b/src/main/java/com/jcraft/jsch/ChannelExec.java @@ -48,7 +48,7 @@ public void start() throws JSchException { } if (io.in != null) { - thread = new Thread(this::run); + thread = _session.getThreadFactory().newThread(this::run); thread.setName("Exec thread " + _session.getHost()); if (_session.daemon_thread) { thread.setDaemon(_session.daemon_thread); diff --git a/src/main/java/com/jcraft/jsch/ChannelForwardedTCPIP.java b/src/main/java/com/jcraft/jsch/ChannelForwardedTCPIP.java index 15de3829..49566b93 100644 --- a/src/main/java/com/jcraft/jsch/ChannelForwardedTCPIP.java +++ b/src/main/java/com/jcraft/jsch/ChannelForwardedTCPIP.java @@ -67,7 +67,7 @@ public void run() { daemon.setChannel(this, getInputStream(), out); daemon.setArg(_config.arg); - new Thread(daemon).start(); + getSession().getThreadFactory().newThread(daemon).start(); } else { ConfigLHost _config = (ConfigLHost) config; socket = diff --git a/src/main/java/com/jcraft/jsch/ChannelShell.java b/src/main/java/com/jcraft/jsch/ChannelShell.java index 7fc3d3d3..c3f72045 100644 --- a/src/main/java/com/jcraft/jsch/ChannelShell.java +++ b/src/main/java/com/jcraft/jsch/ChannelShell.java @@ -48,7 +48,7 @@ public void start() throws JSchException { } if (io.in != null) { - thread = new Thread(this::run); + thread = _session.getThreadFactory().newThread(this::run); thread.setName("Shell for " + _session.host); if (_session.daemon_thread) { thread.setDaemon(_session.daemon_thread); diff --git a/src/main/java/com/jcraft/jsch/ChannelSubsystem.java b/src/main/java/com/jcraft/jsch/ChannelSubsystem.java index 537ae391..72d447f5 100644 --- a/src/main/java/com/jcraft/jsch/ChannelSubsystem.java +++ b/src/main/java/com/jcraft/jsch/ChannelSubsystem.java @@ -64,7 +64,7 @@ public void start() throws JSchException { throw new JSchException("ChannelSubsystem", e); } if (io.in != null) { - thread = new Thread(this::run); + thread = _session.getThreadFactory().newThread(this::run); thread.setName("Subsystem for " + _session.host); if (_session.daemon_thread) { thread.setDaemon(_session.daemon_thread); diff --git a/src/main/java/com/jcraft/jsch/Session.java b/src/main/java/com/jcraft/jsch/Session.java index 4d364d74..33111717 100644 --- a/src/main/java/com/jcraft/jsch/Session.java +++ b/src/main/java/com/jcraft/jsch/Session.java @@ -38,8 +38,10 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Properties; import java.util.Vector; +import java.util.concurrent.ThreadFactory; import javax.crypto.AEADBadTagException; public class Session { @@ -174,6 +176,8 @@ public class Session { JSch jsch; Logger logger; + private ThreadFactory threadFactory = Thread::new; + Session(JSch jsch, String username, String host, int port) throws JSchException { super(); this.jsch = jsch; @@ -528,7 +532,7 @@ public void connect(int connectTimeout) throws JSchException { synchronized (lock) { if (isConnected) { - connectThread = new Thread(this::run); + connectThread = getThreadFactory().newThread(this::run); connectThread.setName("Connect thread " + host + " session"); if (daemon_thread) { connectThread.setDaemon(daemon_thread); @@ -2005,7 +2009,7 @@ void run() { channel.getData(buf); channel.init(); - Thread tmp = new Thread(channel::run); + Thread tmp = getThreadFactory().newThread(channel::run); tmp.setName("Channel " + ctyp + " " + host); if (daemon_thread) { tmp.setDaemon(daemon_thread); @@ -2156,6 +2160,27 @@ public void disconnect() { // System.gc(); } + /** + * Sets a thread factory to be used for creating new threads in this instance. + * + * @param threadFactory The thread factory to be used; must not be null + * @throws NullPointerException if the provided thread factory is null + */ + public void setThreadFactory(ThreadFactory threadFactory) { + this.threadFactory = Objects.requireNonNull(threadFactory); + } + + + /** + * Returns the thread factory used by this instance. + * + * @return The thread factory associated with this instance. If no specific thread factory has + * been set, a default thread factory is returned. + */ + public ThreadFactory getThreadFactory() { + return threadFactory; + } + /** * Registers the local port forwarding for loop-back interface. If lport is * 0, the tcp port will be allocated. @@ -2230,7 +2255,7 @@ public int setPortForwardingL(String bind_address, int lport, String host, int r ServerSocketFactory ssf, int connectTimeout) throws JSchException { PortWatcher pw = PortWatcher.addPort(this, bind_address, lport, host, rport, ssf); pw.setConnectTimeout(connectTimeout); - Thread tmp = new Thread(pw::run); + Thread tmp = getThreadFactory().newThread(pw::run); tmp.setName("PortWatcher Thread for " + host); if (daemon_thread) { tmp.setDaemon(daemon_thread); @@ -2243,7 +2268,7 @@ public int setSocketForwardingL(String bindAddress, int lport, String socketPath ServerSocketFactory ssf, int connectTimeout) throws JSchException { PortWatcher pw = PortWatcher.addSocket(this, bindAddress, lport, socketPath, ssf); pw.setConnectTimeout(connectTimeout); - Thread tmp = new Thread(pw::run); + Thread tmp = getThreadFactory().newThread(pw::run); tmp.setName("PortWatcher Thread for " + host); if (daemon_thread) { tmp.setDaemon(daemon_thread);