首页 > 编程语言 > 详细

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

时间:2019-02-13 15:01:48      阅读:160      评论:0      收藏:0      [点我收藏+]

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

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

 

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

原文:https://www.cnblogs.com/yaoyuan2/p/10369662.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!