java Thread 类的源码阅读(oracle jdk1.8)
java线程类的源码分析阅读技巧;
首先阅读thread类重点关注一下几个问题;
1、start() ,启动一个线程是如何实现的?
2、java线程状态机的变化过程以及如何实现的?
3、
1、start方法的源码如下:
new Thread(Runnable);代码内部实际代码如下:
- /**
- * Initializes a Thread.
- *
- * @param g the Thread group
- * @param target the object whose run() method gets called
- * @param name the name of the new Thread
- * @param stackSize the desired stack size for the new thread, or
- * zero to indicate that this parameter is to be ignored.
- * @param acc the AccessControlContext to inherit, or
- * AccessController.getContext() if null
- * @param inheritThreadLocals if {@code true}, inherit initial values for
- * inheritable thread-locals from the constructing thread
- */
- private void init(ThreadGroup g, Runnable target, String name,
- long stackSize, AccessControlContext acc,
- boolean inheritThreadLocals) {
- if (name == null) {
- throw new NullPointerException("name cannot be null");
- }
- this.name = name;
- Thread parent = currentThread();
- SecurityManager security = System.getSecurityManager();
- if (g == null) {
- /* Determine if it's an applet or not */
- /* If there is a security manager, ask the security manager
- what to do. */
- if (security != null) {
- g = security.getThreadGroup();
- }
- /* If the security doesn't have a strong opinion of the matter
- use the parent thread group. */
- if (g == null) {
- g = parent.getThreadGroup();
- }
- }
- /* checkAccess regardless of whether or not threadgroup is
- explicitly passed in. */
- g.checkAccess();
- /*
- * Do we have the required permissions?
- */
- if (security != null) {
- if (isCCLOverridden(getClass())) {
- security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
- }
- }
- g.addUnstarted();
- this.group = g;
- this.daemon = parent.isDaemon();
- this.priority = parent.getPriority();
- if (security == null || isCCLOverridden(parent.getClass()))
- this.contextClassLoader = parent.getContextClassLoader();
- else
- this.contextClassLoader = parent.contextClassLoader;
- this.inheritedAccessControlContext =
- acc != null ? acc : AccessController.getContext();
- this.target = target;
- setPriority(priority);
- if (inheritThreadLocals && parent.inheritableThreadLocals != null)
- this.inheritableThreadLocals =
- ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
- /* Stash the specified stack size in case the VM cares */
- this.stackSize = stackSize;
- /* Set thread ID */
- tid = nextThreadID();
- }
然后看start方法代码如下:
- /**
- * Causes this thread to begin execution; the Java Virtual Machine
- * calls the <code>run</code> method of this thread.
- * <p>
- * The result is that two threads are running concurrently: the
- * current thread (which returns from the call to the
- * <code>start</code> method) and the other thread (which executes its
- * <code>run</code> method).
- * <p>
- * It is never legal to start a thread more than once.
- * In particular, a thread may not be restarted once it has completed
- * execution.
- *
- * @exception IllegalThreadStateException if the thread was already
- * started.
- * @see #run()
- * @see #stop()
- */
- public synchronized void start() {
- /**
- * This method is not invoked for the main method thread or "system"
- * group threads created/set up by the VM. Any new functionality added
- * to this method in the future may have to also be added to the VM.
- *
- * A zero status value corresponds to state "NEW".
- */
- if (threadStatus != 0)
- throw new IllegalThreadStateException();
- /* Notify the group that this thread is about to be started
- * so that it can be added to the group's list of threads
- * and the group's unstarted count can be decremented. */
- group.add(this);
- boolean started = false;
- try {
- start0();
- started = true;
- } finally {
- try {
- if (!started) {
- group.threadStartFailed(this);
- }
- } catch (Throwable ignore) {
- /* do nothing. If start0 threw a Throwable then
- it will be passed up the call stack */
- }
- }
- }
- private native void start0();
由此可见start方法的本质是调用本地方法start0()即进行的系统调用;
2、java线程的状态机的变化情况:重点关注 Thread中的一个静态枚举内部类,State 和Thread中的一个变量 threadStatus 的变化
threadStatus代码如下:
- /* Java thread status for tools,
- * initialized to indicate thread 'not yet started'
- */
- private volatile int threadStatus = 0;
但是奇怪的是发现在Thread类中并没有对threadStatus进行过赋值。故猜测原因是在native方法中对threadStatus进行赋值。因为该变量由volatile修饰,因此在native方法进行变量修改的同时Thread类中就能同时获取到该变量的最新值。
- /**
- * A thread state. A thread can be in one of the following states:
- * <ul>
- * <li>{@link #NEW}<br>
- * A thread that has not yet started is in this state.
- * </li>
- * <li>{@link #RUNNABLE}<br>
- * A thread executing in the Java virtual machine is in this state.
- * </li>
- * <li>{@link #BLOCKED}<br>
- * A thread that is blocked waiting for a monitor lock
- * is in this state.
- * </li>
- * <li>{@link #WAITING}<br>
- * A thread that is waiting indefinitely for another thread to
- * perform a particular action is in this state.
- * </li>
- * <li>{@link #TIMED_WAITING}<br>
- * A thread that is waiting for another thread to perform an action
- * for up to a specified waiting time is in this state.
- * </li>
- * <li>{@link #TERMINATED}<br>
- * A thread that has exited is in this state.
- * </li>
- * </ul>
- *
- * <p>
- * A thread can be in only one state at a given point in time.
- * These states are virtual machine states which do not reflect
- * any operating system thread states.
- *
- * @since 1.5
- * @see #getState
- */
- public enum State {
- /**
- * Thread state for a thread which has not yet started.
- */
- NEW,
- /**
- * Thread state for a runnable thread. A thread in the runnable
- * state is executing in the Java virtual machine but it may
- * be waiting for other resources from the operating system
- * such as processor.
- */
- RUNNABLE,
- /**
- * Thread state for a thread blocked waiting for a monitor lock.
- * A thread in the blocked state is waiting for a monitor lock
- * to enter a synchronized block/method or
- * reenter a synchronized block/method after calling
- * {@link Object#wait() Object.wait}.
- */
- BLOCKED,
- /**
- * Thread state for a waiting thread.
- * A thread is in the waiting state due to calling one of the
- * following methods:
- * <ul>
- * <li>{@link Object#wait() Object.wait} with no timeout</li>
- * <li>{@link #join() Thread.join} with no timeout</li>
- * <li>{@link LockSupport#park() LockSupport.park}</li>
- * </ul>
- *
- * <p>A thread in the waiting state is waiting for another thread to
- * perform a particular action.
- *
- * For example, a thread that has called <tt>Object.wait()</tt>
- * on an object is waiting for another thread to call
- * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
- * that object. A thread that has called <tt>Thread.join()</tt>
- * is waiting for a specified thread to terminate.
- */
- WAITING,
- /**
- * Thread state for a waiting thread with a specified waiting time.
- * A thread is in the timed waiting state due to calling one of
- * the following methods with a specified positive waiting time:
- * <ul>
- * <li>{@link #sleep Thread.sleep}</li>
- * <li>{@link Object#wait(long) Object.wait} with timeout</li>
- * <li>{@link #join(long) Thread.join} with timeout</li>
- * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
- * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
- * </ul>
- */
- TIMED_WAITING,
- /**
- * Thread state for a terminated thread.
- * The thread has completed execution.
- */
- TERMINATED;
- }
- /**
- * Returns the state of this thread.
- * This method is designed for use in monitoring of the system state,
- * not for synchronization control.
- *
- * @return this thread's state.
- * @since 1.5
- */
- public State getState() {
- // get current thread state
- return sun.misc.VM.toThreadState(threadStatus);
- }
java Thread 类的源码阅读(oracle jdk1.8)的更多相关文章
- Java多线程类FutureTask源码阅读以及浅析
FutureTask是一个具体的实现类,实现了RunnableFuture接口,RunnableFuture分别继承了Runnable和Future接口,因此FutureTask类既可以被线程执行,又 ...
- 《java.util.concurrent 包源码阅读》13 线程池系列之ThreadPoolExecutor 第三部分
这一部分来说说线程池如何进行状态控制,即线程池的开启和关闭. 先来说说线程池的开启,这部分来看ThreadPoolExecutor构造方法: public ThreadPoolExecutor(int ...
- 25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment
25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment 25 BasicUsageEnvironment0基本使用环境基类— ...
- 24 UsageEnvironment使用环境抽象基类——Live555源码阅读(三)UsageEnvironment
24 UsageEnvironment使用环境抽象基类——Live555源码阅读(三)UsageEnvironment 24 UsageEnvironment使用环境抽象基类——Live555源码阅读 ...
- 12 哈希表相关类——Live555源码阅读(一)基本组件类
12 哈希表相关类--Live555源码阅读(一)基本组件类 这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 ...
- 《java.util.concurrent 包源码阅读》 结束语
<java.util.concurrent 包源码阅读>系列文章已经全部写完了.开始的几篇文章是根据自己的读书笔记整理出来的(当时只阅读了部分的源代码),后面的大部分都是一边读源代码,一边 ...
- 18 TaskScheduler任务调度器抽象基类——Live555源码阅读(一)任务调度相关类
这是Live555源码阅读的第二部分,包括了任务调度相关的三个类.任务调度是Live555源码中很重要的部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/ol ...
- 13 HashTable抽象哈希表类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- 11 AlarmHandler定时处理类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
随机推荐
- 芯灵思Sinlinx A64 linux 通过设备树写LED驱动(附参考代码,未测试)
开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 全志A64设备树结构体 #inc ...
- JIT(Just in time,即时编译,边运行边编译)、AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
JIT(Just in time,即时编译,边运行边编译).AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
- 剑指offer 9.递归和循环 变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 这道题还是编程题? 数学渣渣看到心拔凉拔凉的, 要用到数学归纳法来 ...
- upstream timed out (10060: A connection attempt failed because the connected party did not properly respond
openresty 错误日志报错内容: // :: [error] #: * upstream timed : A connection attempt failed because the conn ...
- Xshell5 提示要继续使用此程序,您必须应用最新的更新或使用新版本
Xshell5 提示要继续使用此程序,您必须应用最新的更新或使用新版本 目录 问题描述 修改系统时间 安装新版本 下载 安装 回到顶部 问题描述 今天上线一个服务,上完准备看下OPS的发布结果,就 ...
- elasticSearch 2.3 delete-by-query plugin
The delete-by-query plugin adds support for deleteing all of the documents which match the specified ...
- YARN简述
YARN(Yet Another Resource Negotiator)是Hadoop的集群资源管理系统.YARN提供请求和使用集群资源的API,但这些API很少直接用于用户代码.相反,用户代码中用 ...
- Lubuntu下小巧好用的Python编辑工具Geany
在Lubuntu系统上有很多的Python编辑器,如eclipse,PyCharm等,功能繁多,市场占有率高,但动辄几百M的体积有些巨大,博主今天推荐一款精简且易用的Python开发工具--Geany ...
- Cleanmymac X好不好用?
Cleanmymac X是一款Mac清理软件,能够帮助我们快速实现快速实现磁盘清理,是mac用户的智能助手.那么为什么大家会认定它并坚定不移呢?小编带你感受感受. 1. 简洁大气的外观. 用户正版官方 ...
- catkin-make: command not found 错误解决
参考网址:https://answers.ros.org/question/212492/catkin_make-command-not-found/ zc@ubuntu:~ $ source /op ...