开启线程 关闭线程
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package com.ruoyi.schedule;
|
||||
|
||||
import com.ruoyi.schedule.plc.PlcRead;
|
||||
import com.ruoyi.schedule.plc.domain.CustomThread;
|
||||
import com.ruoyi.schedule.plc.domain.SsjDevice;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@@ -11,23 +11,45 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@Component
|
||||
public class ScheduleThread {
|
||||
|
||||
public PlcRead plcRead;
|
||||
|
||||
public CustomThread customThread;
|
||||
|
||||
/**
|
||||
* 项目初始化启动
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
public void init() {
|
||||
ConcurrentHashMap<String, SsjDevice> list = new ConcurrentHashMap<>();
|
||||
plcRead = new PlcRead(list);
|
||||
PlcRead plcRead = new PlcRead(list);
|
||||
Thread plcReadThread = new Thread(plcRead);
|
||||
customThread = new CustomThread(plcReadThread, plcRead);
|
||||
}
|
||||
|
||||
public void startAllThread(){
|
||||
Thread plcReadThread = new Thread(plcRead);
|
||||
plcReadThread.start();
|
||||
/**
|
||||
* 开始线程
|
||||
*/
|
||||
public void startAllThread() {
|
||||
customThread.getThread().start();
|
||||
}
|
||||
|
||||
public void stopAllThread(){
|
||||
Thread plcReadThread = new Thread(plcRead);
|
||||
plcReadThread.start();
|
||||
/**
|
||||
* 停止线程
|
||||
*/
|
||||
public void stopAllThread() {
|
||||
stopThread(customThread);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止线程
|
||||
*/
|
||||
private void stopThread(CustomThread customThread) {
|
||||
if (customThread.getThread() != null && customThread.getThread().isAlive()) {
|
||||
((PlcRead)customThread.getRunnable()).stopReading();
|
||||
try {
|
||||
customThread.getThread().join(); // 等待线程结束
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user