Java 并发核心知识体系

笔记

Java 并发核心知识体系精讲

一. 线程八大核心

第3章:实现线程的方式:

Introduction

thread1

thread1

thread1

第4章:线程启动

thread1

直接调用run()是在主线程或者当前子线程执行。而start()方法,先检查线程的状态,通过native方法start0()再启动新线程。

第5章:如何正确停止线程

1. 原理介绍:使用interrupt来通知,而不是强制。

2.

thread1

thread1

3.

thread1

4.

thread1

5.

thread1

thread1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Create : 2020/7/31
* Describe : 注意Thread.interrupted()方法的目标对象是"当前线程",而不管本方法来自于哪个对象
*/
public class RightWayInterrupted {
public static void main(String[] args) throws InterruptedException {
Thread threadOne = new Thread(new Runnable() {
@Override
public void run() {
for (; ; ) {

}
}
});

threadOne.start();
threadOne.interrupt();
// 获取中断标志
System.out.println("isInterrupted:" + threadOne.isInterrupted());
// 获取中断标志并重置
// 因为interrupted()是静态方法,不管是threadOne还是Thread,
// 执行它的都是主函数main函数,main函数没有经过任何的中断,所以返回false。
System.out.println("isInterrupted:" + threadOne.interrupted());
// 获取中断标志并重置 原理同上
System.out.println("isInterrupted:" + Thread.interrupted());
// 获取中断标志
System.out.println("isInterrupted:" + threadOne.isInterrupted());
threadOne.join();
System.out.println("Main thread is over");

/**
* 打印结果:
*
* isInterrupted:true
* isInterrupted:false
* isInterrupted:false
* isInterrupted:true
*/
}
}

6.线程的 6个状态(生命周期)

  1. Java线程的6种状态及切换(透彻讲解):https://blog.csdn.net/pange1991/article/details/53860651

2.

thread1

3.

  1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法。

  2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running)两种状态笼统的称为“运行”。

线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。

  1. 阻塞(BLOCKED):表示线程阻塞于锁。一定是Synchronized修饰的代码块或者方法才有这个状态。

  2. 等待(WAITING):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)。

  3. 超时等待(TIMED_WAITING):该状态不同于WAITING,它可以在指定的时间后自行返回。

  4. 终止(TERMINATED):表示该线程已经执行完毕。

4.

thread1

5.

thread1

6.

thread1

根据上面第4点的图解答即可。

第7章 核心5:趣解Thread和Object类中线程相关方法

1.

thread1

2.

thread1

3.

thread1

4.

thread1

thread1

5.

thread1

6.

thread1

7.

thread1

8.

thread1

9.

thread1

第8章 核心6:一网打尽线程属性

thread1

thread1

第10章 核心8:追寻并发的崇高理想-线程安全

thread1

本文配套用于慕课网悟空老师的《Java并发核心知识精讲》课,本文所涵盖的知识导图的内容,在课程中有详细讲解。

课程地址:https://coding.imooc.com/class/362.html

以下思维导图会持续更新,所以请访问线上版本:

线程8大核心基础:

http://naotu.baidu.com/file/07f437ff6bc3fa7939e171b00f133e17?token=6744a1c6ca6860a0

Java内存模型——底层原理:

http://naotu.baidu.com/file/60a0bdcaca7c6b92fcc5f796fe6f6bc9?token=bcdbae34bb3b0533

死锁——从产生到消除:

http://naotu.baidu.com/file/ec7748c253f4fc9d88ac1cc1e47814f3?token=bb71b5895a747d67

二级大纲(复习用) 成体系的Java并发多线程核心+内存模型+死锁——从用法到原理,面试必考:

http://naotu.baidu.com/file/29942292cd032adfae23c09783676004?token=130df3d389cab703

并发工具类分类:

http://naotu.baidu.com/file/3902a010470d7c1cf76fe719be124797?token=bec25304cc071bf1