zoukankan      html  css  js  c++  java
  • 线程池shutdown与shutdownnow 小例子

    如何理解shutdonw与shutdownnow,直接看一下我写的这个小例子:

    //线程生产厂
    public class MyThreadFactory implements ThreadFactory {
    
        private static AtomicInteger ai = new AtomicInteger(0);
    
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r,"-my thread-" + ai.getAndIncrement());
            return t;
        }
    }

    //执行逻辑部分
    public class InterruptThreadInPoolTest implements Runnable {
    
    
        @Override
        public void run(){
            System.out.println( Thread.currentThread().getName()+ " logic start ");
    
             // 核心部分
            if (Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName() + " was interrupted ....");
                //异常有了要记日志还是事物回滚 还不是由你掌握 - -
                throw new NullPointerException("随便丢出去一个异常!");
            }
    
            System.out.println(Thread.currentThread().getName() + " logic end");
        }
    
        public static void main(String[] args) {
            ThreadPoolExecutor t = new ThreadPoolExecutor(5
                    , 5
                    , 1000, TimeUnit.SECONDS
                    , new ArrayBlockingQueue<>(5), new MyThreadFactory());
            for (int i = 0; i < 5; i++) {
                t.submit(new InterruptThreadInPoolTest());
            }
            t.shutdownNow();
            //t.shutdown();
    
        }
    }

     执行完小例子,感觉秒懂呢。

  • 相关阅读:
    数据库中Schema(模式)概念的理解
    debug --- 使用Eclipse
    pgsql 相关函数
    浏览器显示页面排版错误
    jqury 属性
    节点互换需要克隆
    mysql数据库允许远程访问
    request与response的编码和解码
    文本和属性 radio,checkbox,select
    js 和 JQuery 获取iframe的父子值
  • 原文地址:https://www.cnblogs.com/c--k/p/12934605.html
Copyright © 2011-2022 走看看