From 122d2244515f025b2d4891200f14f85020656b81 Mon Sep 17 00:00:00 2001 From: rusefi Date: Mon, 16 May 2022 10:08:39 -0400 Subject: [PATCH] https://github.com/Fazecast/jSerialComm/issues/308 --- .../com/fazecast/jSerialComm/SerialPort.java | 8 +++---- .../jSerialComm/SerialPortThreadFactory.java | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/fazecast/jSerialComm/SerialPortThreadFactory.java 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; + } +}