From 330fae0d2babf8df5ebc51aba4ecdfef45b4abff Mon Sep 17 00:00:00 2001 From: sunxudong Date: Sun, 26 Apr 2020 11:26:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BA=BF=E7=A8=8B=E4=BF=9D?= =?UTF-8?q?=E6=B4=BB=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sheedon/serial/serialport/SafeThread.java | 90 +++++++++++++++++++ .../sheedon/serial/serialport/SerialPort.java | 52 +++-------- 2 files changed, 100 insertions(+), 42 deletions(-) create mode 100644 serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java diff --git a/serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java b/serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java new file mode 100644 index 0000000..7cfb03a --- /dev/null +++ b/serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java @@ -0,0 +1,90 @@ +package org.sheedon.serial.serialport; + +import org.sheedon.serial.NamedRunnable; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * 定时任务保活 + * + * @Author: sheedon + * @Email: sheedonsun@163.com + * @Date: 2020/4/26 10:34 + */ +public class SafeThread { + private final static SafeThread INSTANCE = new SafeThread(); + + private Thread thread; + private int interval; + + private Timer pingTimer; + private TimerTask pingTask; + + private OnThreadHandleListener listener; + + private SafeThread() { + } + + public static SafeThread getInstance() { + return INSTANCE; + } + + public void initConfig(int interval, OnThreadHandleListener listener) { + this.interval = interval; + this.listener = listener; + startThread(); + startPing(); + } + + private void startThread() { + if (thread == null) { + thread = new Thread(runnable); + } + + if (thread.isInterrupted() || !thread.isAlive()) { + thread.start(); + } + } + + private void startPing() { + if (pingTimer == null) { + pingTimer = new Timer(); + } + + if (pingTask == null) { + pingTask = new TimerTask() { + @Override + public void run() { + if (thread == null || thread.isInterrupted() || !thread.isAlive()) { + startThread(); + } + } + }; + } + + pingTimer.schedule(pingTask, 30 * 1000, 30 * 1000); + } + + final NamedRunnable runnable = new NamedRunnable("SafeThread") { + + @Override + protected void execute() { + while (!Thread.currentThread().isInterrupted()) { + + if (listener != null) { + listener.readThread(); + } + try { + Thread.sleep(interval); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }; + + interface OnThreadHandleListener { + void readThread(); + } +} diff --git a/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java b/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java index 44ed63b..9d7734c 100644 --- a/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java +++ b/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java @@ -3,7 +3,6 @@ import org.sheedon.serial.DataCheckBean; import org.sheedon.serial.DataConverter; import org.sheedon.serial.Dispatcher; -import org.sheedon.serial.NamedRunnable; import org.sheedon.serial.SafetyByteBuffer; import org.sheedon.serial.Util; import org.sheedon.serial.internal.CharsUtils; @@ -20,7 +19,7 @@ * @Email: sheedonsun@163.com * @Date: 2020/2/21 9:27 */ -public class SerialPort { +public class SerialPort implements SafeThread.OnThreadHandleListener{ private android.serialport.SerialPort serialPort; private InputStream inputStream; @@ -31,8 +30,6 @@ public class SerialPort { private DataConverter converter; private SafetyByteBuffer serialData = new SafetyByteBuffer(); - private Thread thread; - private int interval; /** @@ -51,7 +48,7 @@ public SerialPort(String path, int baudRate, int flags, int interval, Util.checkNotNull(dispatcher, "dispatcher is null"); this.interval = interval; converter = Util.checkNotNull(dispatcher.checkDataConverter(), "converter is null"); - SerialRunnable runnable = new SerialRunnable(path); + this.callback = callback; serialPort = new android.serialport.SerialPort(new File(path), baudRate, flags); @@ -59,8 +56,7 @@ public SerialPort(String path, int baudRate, int flags, int interval, inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); - thread = new Thread(runnable); - thread.start(); + SafeThread.getInstance().initConfig(interval,this); } @@ -73,37 +69,14 @@ public void setCallback(SerialRealCallback callback) { this.callback = callback; } - // 串口 Runnable - final class SerialRunnable extends NamedRunnable { - - SerialRunnable(String name) { - super("SerialRunnable %s", name); - } - - @Override - protected void execute() { - while (!Thread.currentThread().isInterrupted()) { - readThread(); - try { - Thread.sleep(interval); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - /** - * 解析数据 - */ - private void readThread() { - - byte[] data = getDataByte(); - if (data == null || callback == null) - return; + @Override + public void readThread() { + byte[] data = getDataByte(); + if (data == null || callback == null) + return; - serialData.append(data); - dealWithData(); - } + serialData.append(data); + dealWithData(); } private volatile boolean isStartDealWithData = false; @@ -192,10 +165,5 @@ public void closeSerialPort() { outputStream = null; } - if (thread != null) { - thread.interrupt(); - thread = null; - } - } }