线程池(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

 

上一篇:C通过JNI反向调用JAVA程序方法


下一篇:java 创建线程