java线程池的使用学习
1. 线程池的创建
线程池的创建使用ThreadPoolExecutor类,有利于编码时更好的明确线程池运行规则。

     //构造函数
         /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }
参数含义
(1) 核心线程数corePoolSize:           保持在池中的线程数
(2) 最大线程数maximumPoolSize
(3) 保活时间keepAliveTime:            线程数大于corePoolSize,闲置线程最大空闲时间
(4) 时间单位unit

(5) 阻塞队列workQueue
java.util.concurrent.BlockingQueue主要实现类有:
- ArrayBlockingQueue: 数组结构有界阻塞队列,FIFO排序。其构造函数必须设置队列长度。
 - LinkedBlockingQueue:链表结构有界阻塞队列,FIFO排序。队列默认最大长度为Integer.MAX_VALUE,故可能会堆积大量请求,导致OOM。

 - PriorityBlockingQueue:支持优先级排序的无界阻塞队列。默认自然顺序排列,可以通过比较器comparator指定排序规则。
 - DelayQueue:支持延时获取元素的无界阻塞队列。队列使用PriorityQueue实现。
 
(6) 线程创建接口threadFactory
- 默认使用Executors.defaultThreadFactory()。
 - 可以自定义ThreadFactory实现或使用第三方实现,方便指定有意义的线程名称。
 
import com.google.common.util.concurrent.ThreadFactoryBuilder;
    ...
    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("my-pool-%d").build();
public class MyThreadFactory implements ThreadFactory {
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;
    MyThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix+"-";
    }
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread( r,namePrefix + threadNumber.getAndIncrement());
        if (t.isDaemon()) {
            t.setDaemon(true);
        }
        if (t.getPriority() != Thread.NORM_PRIORITY) {
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }
}
(7) 饱和策略handler
- ThreadPoolExecutor.AbortPolicy():终止策略(默认) , 抛出java.util.concurrent.RejectedExecutionException异常。
 - ThreadPoolExecutor.CallerRunsPolicy(): 重试添加当前的任务,他会自动重复调用execute()方法。
 - ThreadPoolExecutor.DiscardOldestPolicy(): 抛弃下一个即将被执行的任务,然后尝试重新提交新的任务。最好不和优先级队列一起使用,因为它会抛弃优先级最高的任务。
 - ThreadPoolExecutor.DiscardPolicy(): 抛弃策略, 抛弃当前任务。
 
2. 线程池的运行规则
execute添加任务到线程池:
一个任务通过execute(Runnable)方法被添加到线程池。任务是一个 Runnable类型的对象,任务的执行方法就是 Runnable类型对象的run()方法。
线程池运行规则:
当一个任务通过execute(Runnable)方法添加到线程池时:

如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。
如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。
如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。
如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。
也就是:处理任务的优先级为:
核心线程corePoolSize - > 任务队列workQueue - > 最大线程maximumPoolSize
如果三者都满了,使用handler策略处理该任务。
- 当线程池中的线程数量大于 corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。
 
    // execute方法源码实现(jdk1.8)
    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }
3. 线程池的关闭
通过调用线程池的shutdown或shutdownNow方法来关闭线程池。
- shutdown:将线程池的状态设置成SHUTDOWN状态,然后interrupt空闲线程。
 - shutdownNow:线程池的状态设置成STOP,然后尝试interrupt所有线程,包括正在运行的。
 
关于线程池状态,源码中的注释比较清晰:

再看一下源代码:
    // 在关闭中,之前提交的任务会被执行(包含正在执行的,在阻塞队列中的),但新任务会被拒绝。
    public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            // 状态设置为shutdown
            advanceRunState(SHUTDOWN);
            // interrupt空闲线程
            interruptIdleWorkers();
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        // 尝试终止线程池
        tryTerminate();
    }
其中,interruptIdleWorkers()方法往下调用了interruptIdleWorkers(), 这里w.tryLock()比较关键。
中断之前需要先tryLock()获取worker锁,正在运行的worker tryLock()失败(runWorker()方法会先对worker上锁),故正在运行的worker不能中断。

    // 尝试停止所有正在执行的任务,停止对等待任务的处理,并返回正在等待被执行的任务列表
    public List<Runnable> shutdownNow() {
        List<Runnable> tasks;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            // 状态设置为STOP
            advanceRunState(STOP);
            // 停止所有线程  interruptWorkers逻辑简单些,循环对所有worker调用interruptIfStarted().(interrupt所有线程)
            interruptWorkers();
            tasks = drainQueue();
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
        return tasks;
    }
4. 线程池的使用场合
(1)单个任务处理的时间比较短;
(2)需要处理的任务数量大;
5. 线程池大小的设置
可根据计算任务类型估算线程池设置大小:
cpu密集型:可采用Runtime.avaliableProcesses()+1个线程;
IO密集型:由于阻塞操作多,可使用更多的线程,如2倍cpu核数。
6 实现举例
场景: ftp服务器收到文件后,触发相关搬移/处理操作。
public class FtpEventHandler extends DefaultFtplet {
    @Override
    public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
            throws FtpException, IOException {
        // 获取文件名
        String fileName = request.getArgument();
        Integer index = fileName.lastIndexOf("/");
        String realFileName = fileName.substring(index + 1);
        index = realFileName.lastIndexOf("\\");
        realFileName = realFileName.substring(index + 1);
        // **处理文件**
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 50, 10,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3));
        threadPool.execute(new fileSenderThread(realFileName));
        return FtpletResult.DEFAULT;
    }
}
Spring也提供了ThreadPoolTaskExecutor
     <!--spring.xml配置示例-->
    <bean id="gkTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="allowCoreThreadTimeOut" value="true"/>
        <property name="corePoolSize" value="10"/>
        <property name="maxPoolSize" value="50"/>
        <property name="queueCapacity" value="3"/>
        <property name="keepAliveSeconds" value="10"/>
        <property name="rejectedExecutionHandler"
                  value="#{new java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy()}"/>
        <property name="threadNamePrefix" value="gkTaskExecutor"/>
    </bean>
    //java代码中注入bean
    @Autowired
    @Qualifier("gkTaskExecutor")
    private ThreadPoolTaskExecutor gkTaskExecutor;
end.
java线程池的使用学习的更多相关文章
- 【Java线程池快速学习教程】
		
1. Java线程池 线程池:顾名思义,用一个池子装载多个线程,使用池子去管理多个线程. 问题来源:应用大量通过new Thread()方法创建执行时间短的线程,较大的消耗系统资源并且系统的响应速度变 ...
 - Java线程池学习
		
Java线程池学习 Executor框架简介 在Java 5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java ...
 - Java线程池快速学习教程
		
1. Java线程池 线程池:顾名思义,用一个池子装载多个线程,使用池子去管理多个线程. 问题来源:应用大量通过new Thread()方法创建执行时间短的线程,较大的消耗系统资源并且系统的响应速度变 ...
 - Java线程池学习心得
		
一.普通线程和线程池的对比 new Thread的弊端如下: a. 每次new Thread新建对象性能差.b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或o ...
 - (转载)JAVA线程池管理
		
平时的开发中线程是个少不了的东西,比如tomcat里的servlet就是线程,没有线程我们如何提供多用户访问呢?不过很多刚开始接触线程的开发攻城师却在这个上面吃了不少苦头.怎么做一套简便的线程开发模式 ...
 - 四种Java线程池用法解析
		
本文为大家分析四种Java线程池用法,供大家参考,具体内容如下 http://www.jb51.net/article/81843.htm 1.new Thread的弊端 执行一个异步任务你还只是如下 ...
 - Java线程池的原理及几类线程池的介绍
		
刚刚研究了一下线程池,如果有不足之处,请大家不吝赐教,大家共同学习.共同交流. 在什么情况下使用线程池? 单个任务处理的时间比较短 将需处理的任务的数量大 使用线程池的好处: 减少在创建和销毁线程上所 ...
 - Java线程池带图详解
		
线程池作为Java中一个重要的知识点,看了很多文章,在此以Java自带的线程池为例,记录分析一下.本文参考了Java并发编程:线程池的使用.Java线程池---addWorker方法解析.线程池.Th ...
 - Java线程池详解
		
一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...
 
随机推荐
- shell 脚本 变量使用,取消一个变量,echo
			
1. 用户自定义变量,直接使用,赋值的时候等号两边不能有空格 A=100 echo "\$A = $A" # $是取变量A 中的值 "" 号中 \$ 是转译,此 ...
 - C#利用资源文件设置软件自适应多语言
			
在项目更目录下添加两个资源文件,以适应中英文两种版本,如Resource.zh_CN.resx和 Resource.en-US.resx ,两个资源文件的ID都一样,值分别配置相应的中英文 ...
 - erlang 开发建议
			
* 确保没有任何编译警告 * Erlang中String采用list实现,32位系统中,其1个字符用8个字节的空间(4个保存value, 4个保存指针).因此string速度较慢,空间占用较大 * 在 ...
 - Linux 指令查询帮助
			
man +指令名 例子: man rename
 - DDOS 单例
			
DDOS.H #pragma once //g++ ../../../Main.cpp ../../../DDOS.cpp -lpthread #include <stdio.h> #in ...
 - Windows color
			
设置默认的控制台前景和背景颜色. COLOR [attr] attr 指定控制台输出的颜色属性. 颜色属性由两个十六进制数字指定 -- 第一个对应于背景,第二个对应于前景.每个数字可以为 ...
 - python相关软件安装流程图解——Windows下安装Redis以及可视化工具——Redis-x64-3.2.100——redis-desktop-manager-0.9.3.817
			
https://www.2cto.com/database/201708/666191.html https://github.com/MicrosoftArchive/redis/releases ...
 - tomcat8乱码问题
			
1:注册表里修改 1):找到 HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe 如果 该项下已存在CodePage项,则把值改为十进制” ...
 - Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接
			
Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接 字符串是Python中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...
 - java8 新特性学习笔记
			
Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...