mirror of
https://github.com/youthlql/JavaYouth.git
synced 2026-06-16 04:17:06 +00:00
补充图片
This commit is contained in:
@@ -7,7 +7,7 @@ tags:
|
||||
categories:
|
||||
- Java并发
|
||||
keywords: Java并发,原理,源码
|
||||
description: Java并发体系-第一阶段-多线程基础知识,后续会陆续出更深入的。
|
||||
description: 万字系列长文讲解Java并发-第一阶段-多线程基础知识。
|
||||
cover: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/logo_1.png'
|
||||
top_img: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/blog/top_img.jpg'
|
||||
abbrlink: efc79183
|
||||
@@ -181,7 +181,7 @@ public class ThreadTest1 {
|
||||
|
||||
### Runnable接口构造线程源码
|
||||
|
||||
```
|
||||
```java
|
||||
/*下面是Thread类的部分源码*/
|
||||
|
||||
//1.用Runnable接口创建线程时会进入这个方法
|
||||
@@ -287,7 +287,7 @@ MyThread t2 = new MyThread(); //这个构造函数会默认调用Super();也就
|
||||
|
||||
|
||||
|
||||
```
|
||||
```java
|
||||
//代码从上往下顺序执行
|
||||
public Thread() {
|
||||
init(null, null, "Thread-" + nextThreadNum(), 0);
|
||||
@@ -374,7 +374,7 @@ private void init(ThreadGroup g, Runnable target, String name,
|
||||
|
||||
### 最直观的代码描述
|
||||
|
||||
```
|
||||
```java
|
||||
class Window extends Thread{
|
||||
|
||||
|
||||
@@ -418,7 +418,7 @@ public class WindowTest {
|
||||
|
||||
|
||||
|
||||
```
|
||||
```java
|
||||
class Window1 implements Runnable{
|
||||
|
||||
private int ticket = 100;
|
||||
@@ -645,7 +645,7 @@ public void run() {
|
||||
|
||||
Java 线程在运行的生命周期中的指定时刻只可能处于下面 6 种不同状态的其中一个状态,这几个状态在Java源码中用枚举来表示。
|
||||
|
||||
<img src="image/0005.png">
|
||||
<img src="https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/Source_code/First_stage/0005.png">
|
||||
|
||||
线程在生命周期中并不是固定处于某一个状态而是随着代码的执行在不同状态之间切换。Java 线程状态变迁如下图所示。
|
||||
|
||||
@@ -928,7 +928,7 @@ public static void main(String[] args) {
|
||||
|
||||
## join中断测试
|
||||
|
||||
```
|
||||
```java
|
||||
Thread main = Thread.currentThread();
|
||||
Thread t2 = new Thread() {
|
||||
@Override
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
---
|
||||
title: 'Java并发体系-第三阶段-JUC并发包-[1]'
|
||||
tags:
|
||||
- Java并发
|
||||
- 原理
|
||||
- 源码
|
||||
categories:
|
||||
- Java并发
|
||||
keywords: Java并发,原理,源码
|
||||
description: 万字系列长文讲解-Java并发体系-第三阶段-JUC并发包。JUC在高并发编程中使用频率非常高,这里会详细介绍其用法。
|
||||
cover: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/logo_1.png'
|
||||
top_img: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/blog/top_img.jpg'
|
||||
abbrlink: 5be45d9e
|
||||
date: 2020-10-19 22:13:58
|
||||
---
|
||||
|
||||
|
||||
|
||||
# AtomicXXXFieldUpdater
|
||||
|
||||
> 算是一个小补充
|
||||
@@ -1080,8 +1098,10 @@ public class Video34 {
|
||||
|
||||
### 构造方法
|
||||
|
||||
public Semaphore(int permits)
|
||||
public Semaphore(int permits , boolean fair)
|
||||
```java
|
||||
public Semaphore(int permits)
|
||||
public Semaphore(int permits , boolean fair)
|
||||
```
|
||||
|
||||
|
||||
* `permits`:同一时间可以访问资源的线程数目
|
||||
@@ -1089,16 +1109,18 @@ public class Video34 {
|
||||
|
||||
### 重要方法
|
||||
|
||||
public void acquire() throws InterruptedException
|
||||
public void release()
|
||||
```java
|
||||
public void acquire() throws InterruptedException
|
||||
public void release()
|
||||
```
|
||||
|
||||
|
||||
* `acquire()`:**获取一个许可证**,如果许可证用完了,则陷入阻塞。可以被打断。
|
||||
|
||||
* `release()`:**释放一个许可证**
|
||||
|
||||
acquire(int permits)
|
||||
public void release(int permits)
|
||||
`acquire(int permits)`
|
||||
`public void release(int permits)`
|
||||
|
||||
**acquire多个时,如果没有足够的许可证可用,那么当前线程将被禁用以进行线程调度**,并且处于休眠状态,直到发生两件事情之一:
|
||||
|
||||
@@ -1107,35 +1129,39 @@ public class Video34 {
|
||||
|
||||
**release多个时,会使许可证增多,最终可能超过初始值**
|
||||
|
||||
public boolean tryAcquire(int permits)
|
||||
public boolean tryAcquire(int permits,
|
||||
long timeout,
|
||||
TimeUnit unit)
|
||||
throws InterruptedException
|
||||
```java
|
||||
public boolean tryAcquire(int permits)
|
||||
public boolean tryAcquire(int permits,
|
||||
long timeout,
|
||||
TimeUnit unit)
|
||||
throws InterruptedException
|
||||
```
|
||||
|
||||
|
||||
* 尝试去拿,**拿到返回true**
|
||||
|
||||
### 其他方法
|
||||
|
||||
public int availablePermits()
|
||||
```java
|
||||
public int availablePermits()
|
||||
```
|
||||
|
||||
|
||||
* 返回此信号量中当前可用的许可数。
|
||||
|
||||
protected Collection<Thread> getQueuedThreads()
|
||||
`protected Collection<Thread> getQueuedThreads()`
|
||||
public final int getQueueLength()
|
||||
|
||||
* `getQueuedThreads`返回正在阻塞的线程集合
|
||||
|
||||
* `getQueueLength`返回阻塞获取的线程数
|
||||
|
||||
public void acquireUninterruptibly()
|
||||
public void acquireUninterruptibly(int permits)
|
||||
`public void acquireUninterruptibly()`
|
||||
`public void acquireUninterruptibly(int permits)`
|
||||
|
||||
* 可以`不被打断`获取许可证
|
||||
|
||||
public int drainPermits()
|
||||
`public int drainPermits()`
|
||||
|
||||
* 获取当前全部的许可证目标
|
||||
|
||||
@@ -1300,7 +1326,7 @@ C 9
|
||||
|
||||
## Condition版生产者消费者
|
||||
|
||||
```
|
||||
```java
|
||||
/**
|
||||
* @Author: youthlql-吕
|
||||
* @Date: 2019/9/26 15:03
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,22 @@
|
||||
---
|
||||
title: 'Java并发体系-第二阶段-锁与同步-[1]'
|
||||
tags:
|
||||
- Java并发
|
||||
- 原理
|
||||
- 源码
|
||||
categories:
|
||||
- Java并发
|
||||
keywords: Java并发,原理,源码
|
||||
description: '万字系列长文讲解-Java并发体系-第二阶段,从C++和硬件方面讲解。'
|
||||
cover: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/logo_1.png'
|
||||
top_img: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/blog/top_img.jpg'
|
||||
abbrlink: 230c5bb3
|
||||
date: 2020-10-19 22:09:58
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
> - 本阶段文章讲的略微深入,一些基础性问题不会讲解,如有基础性问题不懂,可自行查看我前面的文章,或者自行学习。
|
||||
> - 本篇文章比较适合校招和社招的面试,笔者在2020年面试的过程中,也确实被问到了下面的一些问题。
|
||||
|
||||
@@ -165,7 +184,7 @@ jcstress是java并发压测工具。https://wiki.openjdk.java.net/display/CodeTo
|
||||
|
||||
|
||||
|
||||
```
|
||||
```java
|
||||
import org.openjdk.jcstress.annotations.*;
|
||||
import org.openjdk.jcstress.infra.results.I_Result;
|
||||
|
||||
@@ -576,7 +595,7 @@ volatile不保证原子性,只保证可见性和禁止指令重排
|
||||
|
||||
|
||||
|
||||
```
|
||||
```java
|
||||
private static volatile SingletonDemo instance = null;
|
||||
|
||||
private SingletonDemo() {
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
---
|
||||
title: 'Java并发体系-第二阶段-锁与同步-[2]'
|
||||
tags:
|
||||
- Java并发
|
||||
- 原理
|
||||
- 源码
|
||||
categories:
|
||||
- Java并发
|
||||
keywords: Java并发,原理,源码
|
||||
description: '万字系列长文讲解-Java并发体系-第二阶段,从C++和硬件方面讲解。'
|
||||
cover: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/logo_1.png'
|
||||
top_img: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/blog/top_img.jpg'
|
||||
abbrlink: '8210870'
|
||||
date: 2020-10-19 22:10:58
|
||||
---
|
||||
|
||||
|
||||
|
||||
# 可见性设计的硬件
|
||||
|
||||
<img src="https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/Source_code/Second_stage/0016.png">
|
||||
@@ -64,7 +82,7 @@ isRunning = false; => 写volatile变量,就会通过执行一个内存屏障
|
||||
|
||||
内存屏障是被插入两个CPU指令之间的一种指令,用来禁止处理器指令发生重排序(像屏障一样),从而保障有序性的。另外,为了达到屏障的效果,它也会使处理器写入、读取值之前,将写缓冲器的值写入高速缓存,清空无效队列,实现可见性。
|
||||
|
||||
举例:将写缓冲器数据写入高速缓存,能够避免不同处理器之间不能访问写缓冲器而导致的可见性问题,以及有效地避免了存储转发问题;清空无效队列保证该处理器上高速缓存中不存在旧的副本,进而拿到最新数据
|
||||
举例:将写缓冲器数据写入高速缓存,能够避免不同处理器之间不能访问写缓冲器而导致的可见性问题,以及有效地避免了存储转发问题;清空无效队列保证该处理器上高速缓存中不存在旧的副本,进而拿到最新数据
|
||||
|
||||
## 基本内存屏障
|
||||
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
---
|
||||
title: 'Java并发体系-第二阶段-锁与同步-[3]'
|
||||
tags:
|
||||
- Java并发
|
||||
- 原理
|
||||
- 源码
|
||||
categories:
|
||||
- Java并发
|
||||
keywords: Java并发,原理,源码
|
||||
description: '万字系列长文讲解-Java并发体系-第二阶段,从C++和硬件方面讲解。'
|
||||
cover: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/Java_concurrency/logo_1.png'
|
||||
top_img: 'https://cdn.jsdelivr.net/gh/youthlql/lql_img/blog/top_img.jpg'
|
||||
abbrlink: 113a3931
|
||||
date: 2020-10-19 22:10:58
|
||||
---
|
||||
|
||||
|
||||
|
||||
# synchronized保证三大特性
|
||||
|
||||
**synchronized保证原子性的原理**
|
||||
@@ -75,7 +93,7 @@ synchronized的锁对象中有一个计数器(recursions变量)会记录线
|
||||
|
||||
### synchronized不可中断演示
|
||||
|
||||
```
|
||||
```java
|
||||
public class Test {
|
||||
private static Object obj = new Object();
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
@@ -243,7 +261,7 @@ public class SyncTest {
|
||||
|
||||
使用javap对其进行反汇编,部分信息如下
|
||||
|
||||
```
|
||||
```java
|
||||
{
|
||||
public void syncBlock();
|
||||
descriptor: ()V
|
||||
@@ -407,7 +425,7 @@ Mark Word用于存储对象自身的运行时数据,如哈希码(HashCode)
|
||||
|
||||
## 查看Java对象布局的方法
|
||||
|
||||
```
|
||||
```java
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jol</groupId>
|
||||
<artifactId>jol-core</artifactId>
|
||||
@@ -544,7 +562,7 @@ case 3:当其他线程进入同步块时,发现已经有偏向的线程了
|
||||
|
||||
我们看个demo,在该demo中重复3次获得锁。
|
||||
|
||||
```
|
||||
```java
|
||||
synchronized(obj){
|
||||
synchronized(obj){
|
||||
synchronized(obj){
|
||||
|
||||
Reference in New Issue
Block a user