zoukankan      html  css  js  c++  java
  • SchuledExecutorService 使用controller控制线程关闭

    1:SchuledExecutorService  使用controller控制线程关闭

    package com.li.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @program: GradleTestUseSubModule
     * @author: Yafei Li
     * @create: 2018-06-16 09:01
     * 调度线程控制器,定时执行
     **/
    @Controller
    public class ScheledThreadController {
        ScheduledExecutorService scheduledExecutorService=new ScheduledThreadPoolExecutor(2);  //指定线程个数
    
        @RequestMapping("/start")
        @ResponseBody
        public void start() {
    
                scheduledExecutorService.scheduleAtFixedRate(new Runnable() {  //以固定频率执行,线程1
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread()+"开启了"+System.currentTimeMillis());
                    }
                }, 10,10,TimeUnit.SECONDS);
    
    
                scheduledExecutorService.scheduleAtFixedRate(new Runnable() {  //以固定频率执行,线程2
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread()+"开启了"+System.currentTimeMillis());
                    }
                }, 10,10,TimeUnit.SECONDS);
        }
    
        @RequestMapping("/stop")
        @ResponseBody
        public String stop() {
            scheduledExecutorService.shutdown();
    
            boolean shutdown = scheduledExecutorService.isShutdown();
            if (shutdown) {
                return "关闭成功";
            }
            return "关闭失败";
        }
    }
  • 相关阅读:
    GCD
    hexo 部署 githubPage 部署不了的问题
    vim 常见操作
    linux 常见目录的作用
    cd 简化命令
    linux 系统运行级别
    APNIC IP 库
    linux 安装ssh
    linux
    Android知识点总结
  • 原文地址:https://www.cnblogs.com/liyafei/p/9189855.html
Copyright © 2011-2022 走看看