package com.ruoyi.schedule; import com.ruoyi.device.ssj.domain.DeviceSsj; import com.ruoyi.device.ssj.service.IDeviceSsjService; import com.ruoyi.schedule.plcRead.ssj.domain.PlcConnectInfo; import com.ruoyi.schedule.plcRead.ssj.PlcRead; import com.ruoyi.schedule.plcRead.ssj.domain.CustomThread; import com.ruoyi.schedule.scheduleTask.ssj.bs.BsInSsjThread; import com.ruoyi.schedule.scheduleTask.ssj.js.JsInSsjThread; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @Component public class ScheduleThread { public CustomThread customThread; @Autowired private IDeviceSsjService deviceSsjService; public CustomThread bsInSsjThread; public CustomThread jsInSsjThread; /** * 项目初始化启动 */ @PostConstruct public void init() { String ip = "127.0.0.1"; int port = 102; PlcConnectInfo ssjPlcConnectInfo = new PlcConnectInfo(ip, port); ConcurrentHashMaplist = new ConcurrentHashMap<>(); //获取要去取的输送机 List deviceSsjs = deviceSsjService.selectDeviceSsjList(new DeviceSsj()); for (DeviceSsj deviceSsj : deviceSsjs) { list.put(deviceSsj.getDeviceCode(),deviceSsj); } PlcRead plcRead = new PlcRead(list,ssjPlcConnectInfo); Thread plcReadThread = new Thread(plcRead); customThread = new CustomThread(plcReadThread, plcRead); //调度初始化 BsInSsjThread bsInSsjThread1 = new BsInSsjThread(); bsInSsjThread = new CustomThread(new Thread(bsInSsjThread1),bsInSsjThread1); JsInSsjThread jsInSsjThread1 = new JsInSsjThread(); jsInSsjThread = new CustomThread(new Thread(jsInSsjThread1),bsInSsjThread1); } /** * 开始线程 */ public void startAllThread() { customThread.getThread().start(); } public void startAllThread2() { bsInSsjThread.getThread().start(); jsInSsjThread.getThread().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(); } } } }