package com.muyu.server.util;

import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程
 * @Author：yang
 * @Package：com.muyu.server.util
 * @Project：cloud-property
 * @name：Thread
 * @Date：2024/8/24 21:01
 */
@WebListener
public class Thread {
    @WebListener
    public class ThreadManagerListener implements ServletContextListener {

        private ExecutorService executorService;

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            // 初始化并启动线程
            executorService = Executors.newSingleThreadExecutor();
            executorService.submit(() -> {
                // 你的线程任务
            });
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // 停止线程
            executorService.shutdownNow();
        }
    }
}
