Merge pull request #430 from rusefi/threadfactory

thread factory API
This commit is contained in:
Will Hedgecock 2022-05-16 09:47:05 -05:00 committed by GitHub
commit df9d6da04f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -405,8 +405,8 @@ public final class SerialPort
catch (Exception e) { e.printStackTrace(); }
// Add a shutdown hook to ensure that all ports get closed
Runtime.getRuntime().addShutdownHook(new Thread()
{
Runtime.getRuntime().addShutdownHook(SerialPortThreadFactory.get().newThread(new Runnable() {
@Override
public void run()
{
// Run any user-specified shutdown hooks
@ -423,7 +423,7 @@ public final class SerialPort
isShuttingDown = true;
uninitializeLibrary();
}
});
}));
}
// Static symbolic link testing function
@ -1756,7 +1756,7 @@ public final class SerialPort
dataPacketIndex = 0;
setEventListeningStatus(portHandle, true);
serialEventThread = new Thread(new Runnable()
serialEventThread = SerialPortThreadFactory.get().newThread(new Runnable()
{
@Override
public void run()

View File

@ -0,0 +1,24 @@
package com.fazecast.jSerialComm;
import java.util.concurrent.ThreadFactory;
public class SerialPortThreadFactory {
private static ThreadFactory INSTANCE = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
};
public static ThreadFactory get() {
return INSTANCE;
}
/**
* Use this method to supply custom thread factory
* @param threadFactory
*/
public static void set(ThreadFactory threadFactory) {
SerialPortThreadFactory.INSTANCE = threadFactory;
}
}