Skip to content

Commit

Permalink
增加线程保活定时任务
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxudong committed Apr 26, 2020
1 parent 1ba354f commit 330fae0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 42 deletions.
90 changes: 90 additions & 0 deletions serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java
Original file line number Diff line number Diff line change
@@ -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: [email protected]
* @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();
}
}
52 changes: 10 additions & 42 deletions serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +19,7 @@
* @Email: [email protected]
* @Date: 2020/2/21 9:27
*/
public class SerialPort {
public class SerialPort implements SafeThread.OnThreadHandleListener{

private android.serialport.SerialPort serialPort;
private InputStream inputStream;
Expand All @@ -31,8 +30,6 @@ public class SerialPort {
private DataConverter<SafetyByteBuffer, DataCheckBean> converter;
private SafetyByteBuffer serialData = new SafetyByteBuffer();

private Thread thread;

private int interval;

/**
Expand All @@ -51,16 +48,15 @@ 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);

inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();

thread = new Thread(runnable);
thread.start();
SafeThread.getInstance().initConfig(interval,this);

}

Expand All @@ -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;
Expand Down Expand Up @@ -192,10 +165,5 @@ public void closeSerialPort() {
outputStream = null;
}

if (thread != null) {
thread.interrupt();
thread = null;
}

}
}

0 comments on commit 330fae0

Please sign in to comment.