开启线程 关闭线程
This commit is contained in:
@@ -10,6 +10,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
public class PlcRead implements Runnable{
|
||||
|
||||
private final Map<String, SsjDevice> deviceMap;
|
||||
private volatile boolean running = true;
|
||||
|
||||
public PlcRead(ConcurrentHashMap<String, SsjDevice> deviceMap) {
|
||||
this.deviceMap = deviceMap;
|
||||
@@ -17,7 +18,23 @@ public class PlcRead implements Runnable{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("PLC Read thread is running...");
|
||||
while (running && !Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
// 模拟PLC读取操作
|
||||
Thread.sleep(1000); // 模拟睡眠
|
||||
// TODO: 实际读取PLC设备数据逻辑
|
||||
} catch (InterruptedException e) {
|
||||
// 捕获中断异常,退出循环
|
||||
Thread.currentThread().interrupt(); // 重新设置中断标志
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void stopReading() {
|
||||
running = false;
|
||||
Thread.currentThread().interrupt(); // 主动中断线程
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user