【图灵学院10】高并发之java线程池源码分析
1. 提纲
1)线程池的模块结构
2)示例&原理解析
2. 问题
1)线程池包含哪些东西
2)线程池的运作原理
3)调度线程池的运作原理
4)线程池怎么实现FixRate,FixDelay,他们之间的区别
5)任务怎么取消
3. 源码解析
3.1 线程池框架
接口简介:
java.util.concurrent.Executor:执行器,执行方法
java.util.concurrent.ExecutorService:(执行服务)包含服务的生命周期
java.util.concurrent.ScheduledExecutorService:(调度相关的服务)
辅助类:
java.util.concurrent.Executors:
核心实现类:
java.util.concurrent.ThreadPoolExecutor:(普通的线程池实现类)
java.util.concurrent.ScheduledThreadPoolExecutor:(调度的核心实现类)
java.util.concurrent.ForkJoinPool
完成服务:
java.util.concurrent.CompletionService:
java.util.concurrent.ExecutorCompletionService:
3.2 ThreadPoolExecutor源码剖析
1)构造器
核心数量,任务队列容器,存活时间,线程工厂,处理器
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);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
ThreadPoolExecutor.runWorker()方法源码
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
3.3 ScheduledThreadPoolExecutor
1)
2)FixRate和FixDelay的区别
/**
* Sets the next time to run for a periodic task.
*/
private void setNextRunTime() {
long p = period;
if (p > 0)
time += p;
else
time = triggerTime(-p);
}
FixRate严格按照设定的时间
FixDelay按照上一个线程执行完成的时间间隔
FixRate: 21:40,21:41,21:42
FixDelay:21:40,21:43
void reExecutePeriodic(RunnableScheduledFuture<?> task) {
if (canRunInCurrentRunState(true)) {
//此处没有捕获异常,如果单个任务发生异常,会导致后续任务无法继续执行
super.getQueue().add(task);
if (!canRunInCurrentRunState(true) && remove(task))
task.cancel(false);
else
ensurePrestart();
}
}
【图灵学院10】高并发之java线程池源码分析的更多相关文章
- java线程池源码分析
我们在关闭线程池的时候会使用shutdown()和shutdownNow(),那么问题来了: 这两个方法又什么区别呢? 他们背后的原理是什么呢? 线程池中线程超过了coresize后会怎么操作呢? 为 ...
- java多线程----线程池源码分析
http://www.cnblogs.com/skywang12345/p/3509954.html 线程池示例 在分析线程池之前,先看一个简单的线程池示例. 1 import java.util.c ...
- java多线程——线程池源码分析(一)
本文首发于cdream的个人博客,点击获得更好的阅读体验! 欢迎转载,转载请注明出处. 通常应用多线程技术时,我们并不会直接创建一个线程,因为系统启动一个新线程的成本是比较高的,涉及与操作系统的交互, ...
- Java线程池源码解析
线程池 假如没有线程池,当存在较多的并发任务的时候,每执行一次任务,系统就要创建一个线程,任务完成后进行销毁,一旦并发任务过多,频繁的创建和销毁线程将会大大降低系统的效率.线程池能够对线程进行统一的分 ...
- Java线程池源码及原理
目录 1 说明 1.1类继承图 2 线程池的状态 3 源码分析 3.1完整的线程池构造方法 3.2 ctl 3.3 任务的执行 3.3.1 execute(Runnable command) 3.3. ...
- 线程池之ThreadPoolExecutor线程池源码分析笔记
1.线程池的作用 一方面当执行大量异步任务时候线程池能够提供较好的性能,在不使用线程池的时候,每当需要执行异步任务时候是直接 new 一线程进行运行,而线程的创建和销毁是需要开销的.使用线程池时候,线 ...
- Java并发编程中线程池源码分析及使用
当Java处理高并发的时候,线程数量特别的多的时候,而且每个线程都是执行很短的时间就结束了,频繁创建线程和销毁线程需要占用很多系统的资源和时间,会降低系统的工作效率. 参考http://www.cnb ...
- java线程池源码的理解
线程池 新建线程和切换线程的开销太大了,使用线程池可以节省系统资源. 线程池的关键类:ThreadPoolExecutor. 该类中包含了大量的多线程与并发处理工具,包括ReentrantLock.A ...
- 线程池之ScheduledThreadPoolExecutor线程池源码分析笔记
1.ScheduledThreadPoolExecutor 整体结构剖析. 1.1类图介绍 根据上面类图图可以看到Executor其实是一个工具类,里面提供了好多静态方法,根据用户选择返回不同的线程池 ...
随机推荐
- python第十一天-----补:缓存操作
memcached,首先下载python-memcached模块,在cmd中执行pip install python-memcached即可 memcached比较简单,默认情况仅支持简单的kv存储, ...
- 12-22C#公共控件(基本功能)
在C#窗体中,公共控件的基本功能: 1.获取.设置控件的参数值: 2.事件(其实是一种特殊的方法和属性,当被其他外力触发它,就会发生,类似数据库的触发器.) 下面是基本的公共控件: 1.复选框 1)设 ...
- Android APP使用系统签名
Android M平台在写APP测试使用MediaRecoder通过AudioSource.VOICE_CALL来录制通话上下行音的时候,需要权限 <uses-permission androi ...
- C#windows窗体应用程序如何自适应大小
用C#的windows窗体应用程序做界面十分轻松,但是系统默认是没有让控件跟随窗体的大小改变而已改变的.所以需要我们手动去设置让窗体控件随着窗体的大小改变而改变.所以我们只需要将控件选择 然后把Anc ...
- App.CSharp.Grid的ICells接口
using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Wi ...
- java判断姓是否合格 千家姓
package com.sycx.domain; import java.lang.reflect.Array; public class FirstName { public static bool ...
- python之简单的函数介绍(http://docs.python.org/3/library)
Python不但能非常灵活地定义函数,而且本身内置了很多有用的函数,可以直接调用. 在上面的网站上我们可以进行查询,Python具体都有哪些函数. 我们也可以再交互命令行中来查找函数: >> ...
- adb device offline 解决办法
当电脑中的豌豆荚之类的应用打开的状态下 adb devices 显示连接状态 关闭手机助手之后,adb devices总显示 device offline 后来发现sdk platform-tool ...
- 每天一道算法题(11)——栈的push、pop 序列
题目:输入两个整数序列.其中一个序列表示栈的push 顺序,判断另一个序列有没有可能是对应的pop 顺序.为了简单起见,我们假设push 序列的任意两个整数都是不相等的. 例如:输入的push 序列是 ...
- GitHub Blog创建以及本地管理(转)
GitHub Blog创建以及本地管理 创建 注册GitHub账户 首页点击新建仓库 New repository repository name必须为 Owner.github.io EX:我的 ...