• 线程池(7)创建标准线程池代码


    创建线程池,阿里规范插件要求:

    1、采用ThreadPoolExecutor,而不是Executors

    2、采用自定义ThreadFactory,便于为线程命名。

    @Slf4j
    public class ThreadTest {
        public static void main(String[] args) {
            //创建阻塞队列
            BlockingQueue<Runnable> blockQueue = new ArrayBlockingQueue<Runnable>(20);
            ThreadPoolExecutor pool = new ThreadPoolExecutor(3,5,50,TimeUnit.MILLISECONDS,blockQueue,new CustomThreadFactory());
            
            try {
                for (int i = 0; i < 20; i++) {
                    Runnable syncRunnable = new Runnable() {
                        @Override
                        public void run() {
                            log.info(Thread.currentThread().getName());
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                    pool.execute(syncRunnable);
                }
            } finally {
                pool.shutdown();
            }
        }
    }
    class CustomThreadFactory implements ThreadFactory {
        private AtomicInteger count = new AtomicInteger(1);
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            String threadName = ThreadTest.class.getSimpleName() + count.getAndIncrement();
            System.out.println(threadName);
            t.setName(threadName);
            return t;
        }
    }

    输出:

    ThreadTest1
    ThreadTest2
    ThreadTest3
    2019-02-13 14:21:34.263 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:34.263 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1
    2019-02-13 14:21:34.263 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest3
    2019-02-13 14:21:36.269 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest3
    2019-02-13 14:21:36.270 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1
    2019-02-13 14:21:36.268 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:38.270 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest3
    2019-02-13 14:21:38.270 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:38.272 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1
    2019-02-13 14:21:40.271 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest3
    2019-02-13 14:21:40.272 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:40.273 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1
    2019-02-13 14:21:42.272 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest3
    2019-02-13 14:21:42.272 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:42.273 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1
    2019-02-13 14:21:44.273 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest3
    2019-02-13 14:21:44.273 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:44.274 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1
    2019-02-13 14:21:46.274 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest2
    2019-02-13 14:21:46.275 [] INFO | com.gomepay.goa.oa.api.pm.copy.ThreadTest:24 - ThreadTest1

  • 相关阅读:
    Centos 7环境下配置MySQL 5.7读写分离
    Centos 7环境下安装配置MySQL 5.7
    Hadoop 2.8集群安装及配置记录
    ASP.NET Core 1.1版本之Hello word
    Hadoop版Helloworld之wordcount运行示例
    SSH配置免密登陆设置汇总
    最小安装模式下Centos7.*网卡启动配置
    骚扰式管理
    项目团队之分工协作
    利用微软AntiXss Library过滤输出字符,防止XSS攻击
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/10369662.html
Copyright © 2020-2023  润新知