diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 5d7ed80..af6bbef 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -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 @@ -1755,7 +1755,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() diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortThreadFactory.java b/src/main/java/com/fazecast/jSerialComm/SerialPortThreadFactory.java new file mode 100644 index 0000000..3bbd9ab --- /dev/null +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortThreadFactory.java @@ -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; + } +}