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 "关闭失败";
        }
    }
  • 相关阅读:
    Android控件显示和隐藏
    Android Viewpager+Fragment实现滑动标签页
    Android中的color使用
    Android自定义Button按钮显示样式
    Android通过Intent传递对象
    Android中AsyncTask的使用
    iOS,推送通知
    UIWebView与JS的交互
    iOS蓝牙中的进制转换,数据格式转换
    Core Graphics 定制UIVIew 处理图片
  • 原文地址:https://www.cnblogs.com/liyafei/p/9189855.html