码迷,mamicode.com
首页 > 编程语言 > 详细

十七、ThreadPoolExecutor线程池

时间:2018-12-24 10:26:55      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:handle   epo   link   print   imp   net   void   简介   style   

一、简介

JDK的Executor框架的实现类ThreadPoolExecutor,实现了Executor接口和ExecutorService接口。

ThreadPoolExecutor执行过程如下:

1)判断corePoolSize是否都执行中,如果不是那么直接执行任务。

2)判断缓冲队列是否满了,如果不是那么加入缓冲队列

3)判断是否超过maxNumPoolSize,如果不是那么创建线程执行任务。

4)如果超过maxNumPoolSize那么调用RejectedExecutionHandler

JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/util/concurrent/ThreadPoolExecutor.html

二、代码示例

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo {
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());

    public static void main(String[] args) {
        // 提交线程
        executor.submit(() -> System.out.println("running"));
        System.out.println("end");
    }
}
    

 

十七、ThreadPoolExecutor线程池

标签:handle   epo   link   print   imp   net   void   简介   style   

原文地址:https://www.cnblogs.com/lay2017/p/10166635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!