Java并发编程之线程池
来源:互联网 发布:怎么修改数据库时间 编辑:程序博客网 时间:2024/04/28 22:43
线程是一种稀缺的资源,如果对于每一个任务都来创建一个新的线程来处理,不仅会消耗系统的资源,还会是降低系统的稳定性。使用线程池能够统一的对线程分配与管理。
——-类结构图
1-在接口Executor中,只有一个void execute(Runnable command)
方法
2-ThreadPoolExecutor,多线程的核心类。
3-ScheduledThreadPoolExecutor,继承自ThreadPoolExecutor,增加了任务调度的方法,比Timer更加的强大。
线程池的实现原理
试想,对于一个任务被加到了线程池中,线程池会怎么的来处理这个任务?
下面是ThreadPoolExecutor线程池处理任务的流程:
任务提交线程池
1)首先会先判断核心线程池中的线程是否都处于工作的状态,如果不是,即当前线程数小于核心线程数corePoolSize,则直接创建线程,执行任务。否则,执行下一步
2)加入到阻塞队列的时候,判断队列时候已满, 否,则把任务加到队列,如果是,则执行下一步。
3)判断整个线程池是否已满,否,则创建线程,执行任务,如果是,则进行相关的策略处理(rejectedExecution);
ThreadPoolExecutor的执行示意图:
ThreadPoolExecutor的execute方法源码
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); //阻塞队列为SysnchronousQueue else if (workerCountOf(recheck) == 0) addWorker(null, false); } else if (!addWorker(command, false)) reject(command); }
ThreadPoolExecutor的创建
直接new ThreadPoolExecutor创建的时候,需要注意参数的含义:
public ThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
corePoolSize:核心线程的数目,即线程池中的基本的线程数。
maximumPoolSize:线程池允许的最大的线程数
keepAliveTime:当线程处于空闲状态的时候,超过这个时间,线程将会终止
unit:存活时间的单位。
workQueue:阻塞队列,
有ArrayBlockingQueue:一个基于数组的有界队列
LinkedBlockingQueue:一个基于链表的有界队列,静态方法有Executors.newFixedThreadPool();
SysnchronousQueue:一个不存储元素的阻塞队列,静态方法有Executors.newCachedThreadPool();
priorityBlockingQueue:一个具有优先级的无阻塞队列。
Executors.defaultThreadFactory:默认创建线程的工厂
defaultHandler:策略处理方法,如果线程池处于饱和的状态,则进行相应的策略处理:
AbortPolicy:直接抛出异常(默认)
CallerRunsPolicy:只用调用者所用的线程来处理
DisCardOldestPolicy:丢弃队列中的最近一个任务,执行当前任务
DiscardPoilcy:不处理,直接丢掉。
通过Executors创建线程池
1)创建一个大小固定的线程池
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
corePoolSize=maximumPoolSize=nThread,即核心线程池的数即使最大的线程数目。
keepAliveTime=0L:多余的空余线程会被立即终止
示意图如下:
步骤一:当前运行的线程数小于corePoolSize,创建线程,直接执行。否则,执行下面操作。
步骤二:LinkedBlockingQueue加入任务
步骤三:执行完步骤一的任务后,循环从LinkedBlockingQueue中加入任务
2)创建只有单个线程的线程池
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
corePoolSize=maximumPoolSize=1,线程池中只能有一个线程。
示意图如下:
步骤一:当前运行的线程数小于corePoolSize(即没有运行的线程时),创建线程,直接执行。否则,执行下面操作。
步骤二:LinkedBlockingQueue加入任务
步骤三:执行完步骤一的任务后,循环从LinkedBlockingQueue中加入任务
3)创建CachedThreadPool线程池
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
核心线程数目为0
keepAliveTime=60L 空闲线程60之后将会被终止
示意图如下:
只有线程通过offer提交一个任务,而线程池执行poll操作的时候,匹配成功。
如果线程池中没有线程,则会创建一个线程执行任务。
Java并发编程之volatile解析
- Java并发编程之线程池
- java并发编程:线程池
- Java并发编程:线程池
- Java并发编程 线程池
- java并发编程---线程池
- Java并发编程:线程池
- Java并发编程:线程池
- 【Java并发编程】线程池
- java并发编程--线程池
- Java 并发编程之线程池的使用
- Java 并发编程之线程池的使用 (二)
- Java 并发编程之线程池的使用 (三)
- Java 并发编程之线程池的使用(一)
- Java 并发编程之线程池的使用 (二)
- Java 并发编程之线程池的使用 (三)
- Java并发编程 之 线程池核心ThreadPoolExecutor
- Java并发编程系列之二十五:线程池
- java并发编程学习之 线程池1
- 构造方法
- 表单重复提交解决方法
- 教你彻底学会动态规划(1)
- Java泛型:泛型类、泛型接口和泛型方法
- HTTP协议详解(转)
- Java并发编程之线程池
- 显著性检测总结之Exploiting Local and Global Patch Rarities for Saliency Detection
- POJ 2240 Arbitrage (Floyd最短路变形)
- Python之文件
- 很特别的一个动态规划入门教程
- qt的QTabelWidget控件单元格读取错误的解决方法
- 百度面经+笔经(转自fengsanshao)
- 构造代码块、局部代码块
- 傅里叶变换的由来及复数下的傅里叶变换公式证明