diff --git a/src/main/java/com/fazecast/jSerialComm/NativeSynchronizedInvoker.java b/src/main/java/com/fazecast/jSerialComm/NativeSynchronizedInvoker.java new file mode 100644 index 0000000..b4f1f38 --- /dev/null +++ b/src/main/java/com/fazecast/jSerialComm/NativeSynchronizedInvoker.java @@ -0,0 +1,25 @@ +package com.fazecast.jSerialComm; + +public class NativeSynchronizedInvoker { + public static void testSynchronized() { + System.out.println(SerialPort.getCommPorts()); + + + synchronized (NativeSynchronizedInvoker.class) { + System.out.println("hahaha"); + } + } + + public static void testUnSynchronized() { + System.out.println(SerialPort.getUnSynchronizedCommPorts()); + } + + public static void testCommPortsNotNative() { + System.out.println(SerialPort.getCommPortsNotNative()); + } + + + public static void testNotNativeUnSynchronizedCommPorts() { + System.out.println(SerialPort.getNotNativeUnSynchronizedCommPorts()); + } +} diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index aa08eb6..82b68dc 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -52,6 +52,7 @@ public final class SerialPort // Static initializer loads correct native library for this machine static private final String versionString = "2.9.1"; static private final String tmpdirAppIdProperty = "fazecast.jSerialComm.appid"; + private static final Object GLOBAL_LOCK = new Object(); static private volatile boolean isAndroid = false; static private volatile boolean isWindows = false; static @@ -457,6 +458,18 @@ public final class SerialPort */ static public final synchronized native SerialPort[] getCommPorts(); + static public final native SerialPort[] getUnSynchronizedCommPorts(); + + static public final synchronized SerialPort[] getCommPortsNotNative() { + System.out.println("getCommPortsNotNative"); + return null; + } + + static public final SerialPort[] getNotNativeUnSynchronizedCommPorts(){ + System.out.println("getNotNativeUnSynchronizedCommPorts"); + return null; + } + /** * Allocates a {@link SerialPort} object corresponding to the user-specified port descriptor. *
@@ -585,7 +598,7 @@ public final class SerialPort public final synchronized boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize) { // Synchronize this method to the class scope as well - synchronized (SerialPort.class) + synchronized (GLOBAL_LOCK) { // Set the send/receive internal buffer sizes, and return true if already opened safetySleepTimeMS = safetySleepTime; @@ -685,7 +698,7 @@ public final class SerialPort public final synchronized boolean closePort() { // Synchronize this method to the class scope as well - synchronized (SerialPort.class) + synchronized (GLOBAL_LOCK) { if (serialEventListener != null) serialEventListener.stopListening();