-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support custom ThreadFactory. #793
base: master
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So a few suggestions:
- Instead of using
Executors.defaultThreadFactory()
, let's instead just use a simple lambda basedThread::new
as the default, as this will functionally make this a noop change (sinceExecutors.defaultThreadFactory()
sets a Thread name, etc.). - Let's initialize the new
threadFactory
property to a non-null value and then disallow users from ever setting it to null viasetThreadFactory()
, so that we never have to explicitly check for null ingetThreadFactory()
.
I would also caution against using JSch with virtual threads, at least unless you are using the new Java 24 release that came out earlier this week, since I suspect you would likely encounter problems.
JSch makes significant use of synchronized
style locking, that until the new Java 24 release results in carrier thread pinning when combined with virtual threads, which in turn can lead to difficult to diagnose deadlocks.
@@ -271,6 +273,8 @@ public class JSch { | |||
|
|||
private ConfigRepository configRepository = null; | |||
|
|||
private ThreadFactory threadFactory = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private ThreadFactory threadFactory = null; | |
private ThreadFactory threadFactory = Thread::new; |
public void setThreadFactory(ThreadFactory threadFactory) { | ||
this.threadFactory = threadFactory; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void setThreadFactory(ThreadFactory threadFactory) { | |
this.threadFactory = threadFactory; | |
} | |
public void setThreadFactory(ThreadFactory threadFactory) { | |
this.threadFactory = Objects.requireNonNull(threadFactory); | |
} |
public ThreadFactory getThreadFactory() { | ||
if (threadFactory == null) | ||
threadFactory = Executors.defaultThreadFactory(); | ||
return threadFactory; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public ThreadFactory getThreadFactory() { | |
if (threadFactory == null) | |
threadFactory = Executors.defaultThreadFactory(); | |
return threadFactory; | |
} | |
public ThreadFactory getThreadFactory() { | |
return threadFactory; | |
} |
import java.util.Hashtable; | ||
import java.util.Map; | ||
import java.util.Vector; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import java.util.Hashtable; | |
import java.util.Map; | |
import java.util.Vector; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ThreadFactory; | |
import java.util.Hashtable; | |
import java.util.Map; | |
import java.util.Objects; | |
import java.util.Vector; | |
import java.util.concurrent.ThreadFactory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I am wondering if would it be better to put the new ThreadFactory property in the Session class instead of the JSch class, since the only places that are creating Thread objects are within the Session class or within various Channel implementations, which are strongly bound to a Session object in the first place?
What do you think?
@norrisjeremy Thank you for your reply.
In addition, JDK 21 is being used more and more, especially in high IO usage scenarios where virtual threads are very advantageous. |
This PR adds support for a custom
ThreadFactory
in JSch. Users can now provide their ownThreadFactory
, which is particularly useful in Java 21, as it allows leveraging virtual threads for better concurrency and resource management.