Thread 源码阅读
Thread
- 属性说明
/**
* 程序中的执行线程
* @since 1.0
*/
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
/**
* 线程名称
*/
private volatile String name;
/**
* 线程优先级
*/
private int priority;
/**
* 此线程是否是守护线程
*/
private boolean daemon = false;
/**
* 将运行的目标
*/
private Runnable target;
/**
* 此线程所在的线程组
*/
private ThreadGroup group;
/**
* 当前线程的上下文类加载器
*/
private ClassLoader contextClassLoader;
/**
* 此线程继承的访问控制上下文
*/
private AccessControlContext inheritedAccessControlContext;
/**
* 匿名线程的自增编号
*/
private static int threadInitNumber;
/** 与当前线程绑定的 ThreadLocalMap */
ThreadLocal.ThreadLocalMap threadLocals = null;
/**
* 继承的 ThreadLocalMap
*/
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
/**
* 此线程请求的堆栈大小,如果未设置则由 JVM 自己决定
*/
private long stackSize;
/**
* 此线程的 ID
*/
private long tid;
/**
* 线程名称序列 ID
*/
private static long threadSeqNumber;
/**
* 此线程的状态
*/
private volatile int threadStatus;
/**
* 当前线程在此目标对象 parkBlocker 上阻塞
* java.util.concurrent.locks.LockSupport.park.
*/
volatile Object parkBlocker;
/**
* 阻塞此线程的可中断 IO 对象
*/
private volatile Interruptible blocker;
/**
* 阻塞锁
*/
private final Object blockerLock = new Object();
/**
* 线程的最小优先级
*/
public static final int MIN_PRIORITY = 1;
/**
* 线程的默认优先级
*/
public static final int NORM_PRIORITY = 5;
/**
* 线程的最大优先级
*/
public static final int MAX_PRIORITY = 10;
- 常用静态方法
/**
* 当前线程所在线程组中估计的活跃线程数
*/
public static int activeCount() {
return currentThread().getThreadGroup().activeCount();
}
/**
* 返回当前执行线程
*/
@HotSpotIntrinsicCandidate
public static native Thread currentThread();
/**
* 打印当前线程的堆栈信息,只用于调试
*/
public static void dumpStack() {
new Exception("Stack trace").printStackTrace();
}
/**
* 当前线程是否持有目标对象的监视器
* @since 1.4
*/
public static native boolean holdsLock(Object obj);
/**
* 返回并清空当前线程的中断状态
*
* @revised 6.0
*/
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
/**
* 让当前线程自旋等待一会
* @since 9
*/
@HotSpotIntrinsicCandidate
public static void onSpinWait() {}
/**
* 当前线程休眠指定的毫秒数
*
* @param millis 休眠的毫秒数
*/
public static native void sleep(long millis) throws InterruptedException;
/**
* 当前线程愿意让步处理器时间,从运行状态进入就绪状态
*/
public static native void yield();
- 实例化
/**
* 创建运行目标任务 target 的新线程
*
* @param target 目标 Runnable 任务
*/
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
/**
* 创建运行目标任务 target 的新线程,并指定线程名称,方便排查问题
*
* @param target 目标 Runable 任务
* @param name 新线程的名称
*/
public Thread(Runnable target, String name) {
init(null, target, name, 0);
}
- 常用方法
/**
* 此线程是否还存活
*/
public final native boolean isAlive();
/**
* 此线程是否是守护线程
*/
public final boolean isDaemon() {
return daemon;
}
/**
* 此线程是否已经被设置中断标识,同时清除其中断标识。
*
* @revised 6.0
*/
public boolean isInterrupted() {
return isInterrupted(false);
}
/**
* 返回线程创建时自动生成的ID
*/
public long getId() {
return tid;
}
/**
* 读取此线程的名称
*/
public final String getName() {
return name;
}
/**
* 读取此线程的优先级
*/
public final int getPriority() {
return priority;
}
/**
* 返回此线程的状态
* @since 1.5
*/
public State getState() {
// get current thread state
return jdk.internal.misc.VM.toThreadState(threadStatus);
}
/**
* 线程状态
*/
public enum State {
/**
* 线程刚创建,还未启动
*/
NEW,
/**
* 线程正在运行
*/
RUNNABLE,
/**
* 此线程在等待获取指定对象的监视器,已经阻塞
*/
BLOCKED,
/**
* 此线程在阻塞等待其他线程的信号
*/
WAITING,
/**
* 此线程在超时阻塞等待
*/
TIMED_WAITING,
/**
* 此线程已经终止
*/
TERMINATED;
}
/**
* 读取此线程的调用堆栈
* @since 1.5
*/
public StackTraceElement[] getStackTrace() {
if (this != Thread.currentThread()) {
// check for getStackTrace permission
final SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(
SecurityConstants.GET_STACK_TRACE_PERMISSION);
}
// 线程已经死亡,则返回空的调用堆栈
if (!isAlive()) {
return EMPTY_STACK_TRACE;
}
final StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
StackTraceElement[] stackTrace = stackTraceArray[0];
// a thread that was alive during the previous isAlive call may have
// since terminated, therefore not having a stacktrace.
if (stackTrace == null) {
stackTrace = EMPTY_STACK_TRACE;
}
return stackTrace;
} else {
return new Exception().getStackTrace();
}
}
/**
* 启动当前线程
*/
public synchronized void start() {
/**
* 线程不是新建状态
*/
if (threadStatus != 0) {
throw new IllegalThreadStateException();
}
// 将此线程加入线程组
group.add(this);
boolean started = false;
try {
// 启动线程
start0();
started = true;
} finally {
try {
// 如果启动失败,则将其从线程组中移除
if (!started) {
group.threadStartFailed(this);
}
} catch (final Throwable ignore) {
}
}
}
private native void start0();
/**
* 中断当前线程
*/
public void interrupt() {
if (this != Thread.currentThread()) {
checkAccess();
}
synchronized (blockerLock) {
final Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
// 中断此线程
interrupt0();
}
/**
* 阻塞等待当前线程执行完毕
*/
public final void join() throws InterruptedException {
join(0);
}
/**
* 在指定的毫秒内阻塞等待此线程执行完毕
*
* @param millis 阻塞等待的毫秒数
*/
public final synchronized void join(long millis)
throws InterruptedException {
// 当前毫秒数
final long base = System.currentTimeMillis();
long now = 0;
// 毫秒数非法
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
// 毫秒数为 0,则表示无限期阻塞等待此线程执行完毕
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
// 线程处于存活状态
while (isAlive()) {
// 计算延时
final long delay = millis - now;
if (delay <= 0) {
break;
}
// 阻塞等待
wait(delay);
// 重新计算时间
now = System.currentTimeMillis() - base;
}
}
}
/**
* 在当前线程中执行目标任务
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
Thread 源码阅读的更多相关文章
- 【原】SDWebImage源码阅读(四)
[原]SDWebImage源码阅读(四) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 SDWebImage中主要实现了NSURLConnectionDataDelega ...
- 【原】SDWebImage源码阅读(三)
[原]SDWebImage源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1.SDWebImageDownloader中的downloadImageWithURL 我们 ...
- 源码阅读系列:EventBus
title: 源码阅读系列:EventBus date: 2016-12-22 16:16:47 tags: 源码阅读 --- EventBus 是人们在日常开发中经常会用到的开源库,即使是不直接用的 ...
- EventBus源码解析 源码阅读记录
EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...
- 20 BasicTaskScheduler0 基本任务调度类基类(二)——Live555源码阅读(一)任务调度相关类
这是Live555源码阅读的第二部分,包括了任务调度相关的三个类.任务调度是Live555源码中很重要的部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/ol ...
- Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)
我们到底能走多远系列(33) 扯淡: 各位: 命运就算颠沛流离 命运就算曲折离奇 命运就算恐吓着你做人没趣味 别流泪 心酸 更不应舍弃 ... 主题: Spring源码阅读还在继 ...
- Netty源码阅读(一) ServerBootstrap启动
Netty源码阅读(一) ServerBootstrap启动 转自我的Github Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速 ...
- AQS源码阅读笔记(一)
AQS源码阅读笔记 先看下这个类张非常重要的一个静态内部类Node.如下: static final class Node { //表示当前节点以共享模式等待锁 static final Node S ...
- Mina源码阅读笔记(四)—Mina的连接IoConnector2
接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...
随机推荐
- How to mount remote windows partition (windows share) under Linux
http://www.cyberciti.biz/tips/how-to-mount-remote-windows-partition-windows-share-under-linux.html ...
- 8.7.ZooKeeper Watcher监听
1.ZooKeeper Watcher ZooKeeper 提供了分布式数据发布/订阅功能,一个典型的发布/订阅模型系统定义了一种一对多的订阅关系,能让多个订阅者同时监听某一个主题对象, 当这个主题对 ...
- Cobbler——无人值守安装多种版本多种配置操作系统
Cobbler Web 图形界面也能实现类似的导入功能,可以参见下一节举了一个Cobbler Web的用法: cobbler 添加网络同步仓库(Reposync用法)
- Jenkins配置邮件信息
1.进入Manage Jenkins----系统配置 2.设置Jenkins地址和管理员邮箱(不设置管理员邮箱无法发送邮件) 3.配置系统管理员的邮件属性 4.点击[高级]配置系统管理员的邮件属性 5 ...
- docker下安装运行mysql的过程以mysql5.7为例
一.查找mysql资源 docker search mysql 其实这步顶多是看看有哪些mysql资源,除非你自己commit过一个特定的版本,否则直接执行下一步 二.安装mysql docker p ...
- html 与 jsp 文件格式区别
html 与 jsp 文件格式区别 html5 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- Linux系统挂载存储只读改成读写
Copy from:https://blog.csdn.net/u010977122/article/details/53316671 1.mount:用于查看哪个模块输入只读,一般显示为:[root ...
- C#静态变量 总结
在C#程序中,没有全局变量的概念,这意味着所有的成员变量只有该类的实例才能操作这些数据,这起到了“信息隐藏”的作用.但有些时候,这样做却不是个明智的选择. 假设我们要定义一个图书类,要求该类能保存图书 ...
- [Python]token的生成及验证
hmac模块(仅在python3中可以使用) 简介: HMAC是密钥相关的哈希运算消息认证码,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出. 典型应用: HMAC的一 ...
- 油猴Tampermonkey离线安装流程(附文件)
1.下载插件插件包,然后解压(解压到你想放插件的位置,其实任意位置都可以,记住解压的位置) 链接:https://pan.baidu.com/s/1aanhsb6ZlapnzBeBRtp3Hg 提取码 ...