美文网首页
ScheduledThreadPoolExecutor 关于 s

ScheduledThreadPoolExecutor 关于 s

作者: Unixtrong | 来源:发表于2017-02-28 18:22 被阅读0次
    • shutdown()
      无法直接停止。
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.schedule(new Runnable() {
        @Override
        public void run() {
            System.out.println("+++ executor run.");
        }
    }, 5, TimeUnit.SECONDS);
    new Thread(new Runnable() {
        @Override
        public void run() {
            HCTools.sleep(1, TimeUnit.SECONDS);
            executor.shutdown();
            System.out.println("--- executor shutdown.");
        }
    }).start();
    

    输出结果:

    --- executor shutdown.
    +++ executor run.
    
    • shutdownNow()
      调用后会立即停止。
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.schedule(new Runnable() {
        @Override
        public void run() {
            System.out.println("+++ executor run.");
        }
    }, 5, TimeUnit.SECONDS);
    new Thread(new Runnable() {
        @Override
        public void run() {
            HCTools.sleep(1, TimeUnit.SECONDS);
            executor.shutdownNow();
            System.out.println("--- executor shutdownNow.");
        }
    }).start();
    

    输出结果:

    --- executor shutdownNow.
    

    相关文章

      网友评论

          本文标题:ScheduledThreadPoolExecutor 关于 s

          本文链接:https://www.haomeiwen.com/subject/nyxfgttx.html