diff --git a/docs/notes/Java 并发.md b/docs/notes/Java 并发.md index b79ad371..99c2925d 100644 --- a/docs/notes/Java 并发.md +++ b/docs/notes/Java 并发.md @@ -62,7 +62,7 @@ # 一、线程状态转换 -

+

## 新建(New) @@ -737,7 +737,7 @@ java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J. 维护了一个计数器 cnt,每次调用 countDown() 方法会让计数器的值减 1,减到 0 的时候,那些因为调用 await() 方法而在等待的线程就会被唤醒。 -

+

```java public class CountdownLatchExample { @@ -786,7 +786,7 @@ public CyclicBarrier(int parties) { } ``` -

+

```java public class CyclicBarrierExample { @@ -819,8 +819,6 @@ before..before..before..before..before..before..before..before..before..before.. Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的访问线程数。 -

- 以下代码模拟了对某个服务的并发请求,每次只能有 3 个客户端同时访问,请求总数为 10。 ```java @@ -1025,7 +1023,7 @@ public class ForkJoinPool extends AbstractExecutorService ForkJoinPool 实现了工作窃取算法来提高 CPU 的利用率。每个线程都维护了一个双端队列,用来存储需要执行的任务。工作窃取算法允许空闲的线程从其它线程的双端队列中窃取一个任务来执行。窃取的任务必须是最晚的任务,避免和队列所属线程发生竞争。例如下图中,Thread2 从 Thread1 的队列中拿出最晚的 Task1 任务,Thread1 会拿出 Task2 来执行,这样就避免发生竞争。但是如果队列中只有一个任务时还是会发生竞争。 -

+

# 九、线程不安全示例 @@ -1080,19 +1078,19 @@ Java 内存模型试图屏蔽各种硬件和操作系统的内存访问差异, 加入高速缓存带来了一个新的问题:缓存一致性。如果多个缓存共享同一块主内存区域,那么多个缓存的数据可能会不一致,需要一些协议来解决这个问题。 -

+

所有的变量都存储在主内存中,每个线程还有自己的工作内存,工作内存存储在高速缓存或者寄存器中,保存了该线程使用的变量的主内存副本拷贝。 线程只能直接操作工作内存中的变量,不同线程之间的变量值传递需要通过主内存来完成。 -

+

## 内存间交互操作 Java 内存模型定义了 8 个操作来完成主内存和工作内存的交互操作。 -

+

- read:把一个变量的值从主内存传输到工作内存中 - load:在 read 之后执行,把 read 得到的值放入工作内存的变量副本中 @@ -1115,11 +1113,11 @@ Java 内存模型保证了 read、load、use、assign、store、write、lock 和 下图演示了两个线程同时对 cnt 进行操作,load、assign、store 这一系列操作整体上看不具备原子性,那么在 T1 修改 cnt 并且还没有将修改后的值写入主内存,T2 依然可以读入旧值。可以看出,这两个线程虽然执行了两次自增运算,但是主内存中 cnt 的值最后为 1 而不是 2。因此对 int 类型读写操作满足原子性只是说明 load、assign、store 这些单个操作具备原子性。 -

+

AtomicInteger 能保证多个线程修改的原子性。 -

+

使用 AtomicInteger 重写之前线程不安全的代码之后得到以下线程安全实现: @@ -1477,7 +1475,7 @@ public class ThreadLocalExample1 { 它所对应的底层结构图为: -

+

每个 Thread 都有一个 ThreadLocal.ThreadLocalMap 对象。 diff --git a/docs/notes/pics/066f9c11-0154-42c3-8685-301a70e9bd39.jpg b/docs/notes/pics/066f9c11-0154-42c3-8685-301a70e9bd39.jpg new file mode 100644 index 00000000..84d371b8 Binary files /dev/null and b/docs/notes/pics/066f9c11-0154-42c3-8685-301a70e9bd39.jpg differ diff --git a/docs/notes/pics/3144015c-dcfb-47ac-94a5-bab3b78b0f14.jpg b/docs/notes/pics/3144015c-dcfb-47ac-94a5-bab3b78b0f14.jpg new file mode 100644 index 00000000..a805e7f9 Binary files /dev/null and b/docs/notes/pics/3144015c-dcfb-47ac-94a5-bab3b78b0f14.jpg differ diff --git a/docs/notes/pics/8162aebb-8fd2-4620-b771-e65751ba7e41.png b/docs/notes/pics/8162aebb-8fd2-4620-b771-e65751ba7e41.png new file mode 100644 index 00000000..2fa69582 Binary files /dev/null and b/docs/notes/pics/8162aebb-8fd2-4620-b771-e65751ba7e41.png differ diff --git a/docs/notes/pics/847b9ba1-b3cd-4e52-aa72-dee0222ae09f.jpg b/docs/notes/pics/847b9ba1-b3cd-4e52-aa72-dee0222ae09f.jpg new file mode 100644 index 00000000..c68fd3ef Binary files /dev/null and b/docs/notes/pics/847b9ba1-b3cd-4e52-aa72-dee0222ae09f.jpg differ diff --git a/docs/notes/pics/912a7886-fb1d-4a05-902d-ab17ea17007f.jpg b/docs/notes/pics/912a7886-fb1d-4a05-902d-ab17ea17007f.jpg new file mode 100644 index 00000000..e30c6908 Binary files /dev/null and b/docs/notes/pics/912a7886-fb1d-4a05-902d-ab17ea17007f.jpg differ diff --git a/docs/notes/pics/96706658-b3f8-4f32-8eb3-dcb7fc8d5381.jpg b/docs/notes/pics/96706658-b3f8-4f32-8eb3-dcb7fc8d5381.jpg new file mode 100644 index 00000000..c4e77ba1 Binary files /dev/null and b/docs/notes/pics/96706658-b3f8-4f32-8eb3-dcb7fc8d5381.jpg differ diff --git a/docs/notes/pics/b6a7e8af-91bf-44b2-8874-ccc6d9d52afc.jpg b/docs/notes/pics/b6a7e8af-91bf-44b2-8874-ccc6d9d52afc.jpg new file mode 100644 index 00000000..96b80deb Binary files /dev/null and b/docs/notes/pics/b6a7e8af-91bf-44b2-8874-ccc6d9d52afc.jpg differ diff --git a/docs/notes/pics/d2a12961-2b36-4463-b017-ca46a3308b8e.png b/docs/notes/pics/d2a12961-2b36-4463-b017-ca46a3308b8e.png new file mode 100644 index 00000000..ce6663bc Binary files /dev/null and b/docs/notes/pics/d2a12961-2b36-4463-b017-ca46a3308b8e.png differ diff --git a/docs/notes/pics/e19452dd-220a-4a6b-bcb0-91ad5e5c4706.png b/docs/notes/pics/e19452dd-220a-4a6b-bcb0-91ad5e5c4706.png new file mode 100644 index 00000000..c21e9af2 Binary files /dev/null and b/docs/notes/pics/e19452dd-220a-4a6b-bcb0-91ad5e5c4706.png differ diff --git a/docs/notes/pics/f944fac3-482b-4ca3-9447-17aec4a3cca0.png b/docs/notes/pics/f944fac3-482b-4ca3-9447-17aec4a3cca0.png new file mode 100644 index 00000000..56f05611 Binary files /dev/null and b/docs/notes/pics/f944fac3-482b-4ca3-9447-17aec4a3cca0.png differ diff --git a/docs/pics/066f9c11-0154-42c3-8685-301a70e9bd39.jpg b/docs/pics/066f9c11-0154-42c3-8685-301a70e9bd39.jpg new file mode 100644 index 00000000..84d371b8 Binary files /dev/null and b/docs/pics/066f9c11-0154-42c3-8685-301a70e9bd39.jpg differ diff --git a/docs/pics/3144015c-dcfb-47ac-94a5-bab3b78b0f14.jpg b/docs/pics/3144015c-dcfb-47ac-94a5-bab3b78b0f14.jpg new file mode 100644 index 00000000..a805e7f9 Binary files /dev/null and b/docs/pics/3144015c-dcfb-47ac-94a5-bab3b78b0f14.jpg differ diff --git a/docs/pics/8162aebb-8fd2-4620-b771-e65751ba7e41.png b/docs/pics/8162aebb-8fd2-4620-b771-e65751ba7e41.png new file mode 100644 index 00000000..2fa69582 Binary files /dev/null and b/docs/pics/8162aebb-8fd2-4620-b771-e65751ba7e41.png differ diff --git a/docs/pics/847b9ba1-b3cd-4e52-aa72-dee0222ae09f.jpg b/docs/pics/847b9ba1-b3cd-4e52-aa72-dee0222ae09f.jpg new file mode 100644 index 00000000..c68fd3ef Binary files /dev/null and b/docs/pics/847b9ba1-b3cd-4e52-aa72-dee0222ae09f.jpg differ diff --git a/docs/pics/912a7886-fb1d-4a05-902d-ab17ea17007f.jpg b/docs/pics/912a7886-fb1d-4a05-902d-ab17ea17007f.jpg new file mode 100644 index 00000000..e30c6908 Binary files /dev/null and b/docs/pics/912a7886-fb1d-4a05-902d-ab17ea17007f.jpg differ diff --git a/docs/pics/96706658-b3f8-4f32-8eb3-dcb7fc8d5381.jpg b/docs/pics/96706658-b3f8-4f32-8eb3-dcb7fc8d5381.jpg new file mode 100644 index 00000000..c4e77ba1 Binary files /dev/null and b/docs/pics/96706658-b3f8-4f32-8eb3-dcb7fc8d5381.jpg differ diff --git a/docs/pics/b6a7e8af-91bf-44b2-8874-ccc6d9d52afc.jpg b/docs/pics/b6a7e8af-91bf-44b2-8874-ccc6d9d52afc.jpg new file mode 100644 index 00000000..96b80deb Binary files /dev/null and b/docs/pics/b6a7e8af-91bf-44b2-8874-ccc6d9d52afc.jpg differ diff --git a/docs/pics/d2a12961-2b36-4463-b017-ca46a3308b8e.png b/docs/pics/d2a12961-2b36-4463-b017-ca46a3308b8e.png new file mode 100644 index 00000000..ce6663bc Binary files /dev/null and b/docs/pics/d2a12961-2b36-4463-b017-ca46a3308b8e.png differ diff --git a/docs/pics/e19452dd-220a-4a6b-bcb0-91ad5e5c4706.png b/docs/pics/e19452dd-220a-4a6b-bcb0-91ad5e5c4706.png new file mode 100644 index 00000000..c21e9af2 Binary files /dev/null and b/docs/pics/e19452dd-220a-4a6b-bcb0-91ad5e5c4706.png differ diff --git a/docs/pics/f944fac3-482b-4ca3-9447-17aec4a3cca0.png b/docs/pics/f944fac3-482b-4ca3-9447-17aec4a3cca0.png new file mode 100644 index 00000000..56f05611 Binary files /dev/null and b/docs/pics/f944fac3-482b-4ca3-9447-17aec4a3cca0.png differ