自动化立体仓库 - WCS系统
#
LSH
2024-01-05 1b9e9f54bf084aaa6befe16cacac4092d71442fa
src/main/java/com/zy/core/ServerBootstrap.java
@@ -12,10 +12,16 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * Created by vincent on 2020/8/4
@@ -28,6 +34,8 @@
    private SlaveProperties slaveProperties;
    @Autowired
    private MainProcess mainProcess;
    public static final Map<CrnThread, Thread> map = new ConcurrentHashMap<>();
    /**
     * PostConstruct会在加载servlet的时候运行一次
@@ -74,12 +82,14 @@
        }
    }
    private void initThread(){
    private void initThread() throws InterruptedException {
        // 初始化堆垛机线程
        News.info("初始化堆垛机线程...................................................");
        for (CrnSlave crn : slaveProperties.getCrn()) {
            CrnThread crnThread = new MelsecCrnThread(crn);
            new Thread((Runnable) crnThread).start();
            Thread thread = new Thread((Runnable) crnThread);
            thread.start();
            map.put(crnThread, thread);
            SlaveConnection.put(SlaveType.Crn, crn.getId(), crnThread);
        }
        // 初始化输送线线程
@@ -117,5 +127,42 @@
    public void destroy() {
    }
    @Component
    class killThread {
        @Scheduled(cron = "0/3 * * * * ? ")
        public void kill() {
            int i = 0;
            for (Map.Entry<CrnThread, Thread> entry : map.entrySet()) {
                i++;
                MelsecCrnThread key = (MelsecCrnThread) entry.getKey();
                System.out.println("第"+i+"个堆垛机线程重启线程条件:"+(System.currentTimeMillis() - key.getSign()));
                if (System.currentTimeMillis() - key.getSign() > 120000) {
                    System.out.println("第"+i+"个堆垛机线程被重启");
                    // 请求线程安全停止
                    key.requestStop();
                    // 等待线程完成停止, 可以这样做也可以选择不这么做,取决于是否需要等待线程结束
                    try {
                        entry.getValue().join(20000); // 等待最多20秒
                    } catch (InterruptedException e) {
//                        // 可适当处理中断
//                        Thread.currentThread().interrupt(); // 保留中断状态
                        entry.getValue().stop();
                    }
                    // 重新启动线程
                    Thread thread = new Thread((Runnable) key); // 假设CrnThread实现了Runnable
                    thread.start();
                    // 替换映射关系中的旧线程
                    map.put(entry.getKey(), thread);
                }
            }
        }
    }
}