1
- 内容整理自[ 《Java并发编程实战》] ( https://time.geekbang.org/column/intro/159 )
1
+ 内容整理自[ 《Java并发编程实战》] ( https://time.geekbang.org/column/intro/159 ) [ 《java-concurrency-patterns》 ] ( https://github.com/LeonardoZ/java-concurrency-patterns )
2
2
相关工具类[ 《vjtools》] ( https://github.com/vipshop/vjtools )
3
- WeChat: Zed-RD [ ![ Build Status] ( https://travis-ci.org/Fadezed/concurrency.svg?branch=master )] ( https://travis-ci.org/Fadezed/concurrency )
3
+
4
+ [ ![ Build Status] ( https://travis-ci.org/Fadezed/concurrency.svg?branch=master )] ( https://travis-ci.org/Fadezed/concurrency )
4
5
5
6
6
7
<div align =" center " >
@@ -39,20 +40,18 @@ WeChat:Zed-RD [
44
+ * [ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/ visibility/Visibility.java )
45
45
![ 单核CPU] ( media/15603316282468/a07e8182819e2b260ce85b2167d446da.png )
46
46
![ 多核CPU] ( media/15603316282468/e2aa76928b2bc135e08e7590ca36e0ea.png )
47
47
48
48
49
49
## 线程切换带来的原子性问题
50
50
* 一个或者多个操作在 CPU 执行的过程中不被中断的特性称为** 原子性**
51
-
52
51
* ** 时间片** 概念
53
52
* 线程切换 ---〉提升cpu利用率。 tips: Unix 系统因支持多进程分时复用而出名。
54
- * 线程切换[ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/contentSwitch /ContentSwitchTest.java ) 。
55
- * 原子问题[ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/atomic/AtomicCounter.java )
53
+ * 线程切换[ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/contentswitch /ContentSwitchTest.java ) 。
54
+ * 原子问题[ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/ atomic/AtomicCounter.java )
56
55
57
56
![ 254b129b145d80e9bb74123d6e620efb] ( media/15603316282468/254b129b145d80e9bb74123d6e620efb.png )
58
57
* count+=1 操作分析
@@ -94,7 +93,7 @@ public class Singleton {
94
93
## 按需禁用缓存以及编译优化 [ 代码来源] ( http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html )
95
94
96
95
* ## volatile
97
- * [ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/volatileExample /VolatileExample.java )
96
+ * [ 代码示例] ( https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/volatilecase /VolatileExample.java )
98
97
99
98
* ## synchronized
100
99
@@ -134,9 +133,9 @@ public class Singleton {
134
133
* 偏向锁
135
134
* 轻量级锁
136
135
* 重量级锁
137
-
138
136
139
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/synchronizedEx/SynchronizedExample.java)
137
+
138
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/synchronizedcase/SynchronizedExample.java)
140
139
141
140
```
142
141
class X {
@@ -158,7 +157,7 @@ public class Singleton {
158
157
159
158
## final
160
159
161
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/finalEx /FinalExample.java)
160
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/finalcase /FinalExample.java)
162
161
* 修饰变量时,初衷是告诉编译器:这个变量生而不变,非immutable,即只能表示对象引用不能被赋值(例如List);
163
162
* 修饰方法则方法不能被重写
164
163
* 修饰类则不能被扩展继承。
@@ -238,7 +237,7 @@ public class Singleton {
238
237
239
238
-------
240
239
# 4.JAVA线程的生命周期
241
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/threadState /ThreadState.java)
240
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/threadstate /ThreadState.java)
242
241
243
242
## 通用的线程生命周期
244
243
* 初始状态
@@ -484,7 +483,7 @@ class Semaphore{
484
483
### 使用方法
485
484
#### 实现互斥
486
485
#### 实现限流器(Semaphore 可以允许多个线程访问一个临界区)
487
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/semaphore/SemaphoreEx.java)
486
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/ semaphore/SemaphoreEx.java)
488
487
489
488
490
489
-------
@@ -495,16 +494,16 @@ class Semaphore{
495
494
* 只允许一个线程写共享变量
496
495
* 如果一个写线程正在执行写操作,此时禁止读线程读共享变量
497
496
498
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/readWriteLock /CacheByReadWriteLock.java)
497
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/readwritelock /CacheByReadWriteLock.java)
499
498
500
499
## StampedLock 加上乐观读(无锁)
501
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/readWriteLock /StampedLockEx.java)
500
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/readwritelock /StampedLockEx.java)
502
501
503
502
## CountDownLatch
504
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/countDownLatchEx /CountDownLatchEx.java)
503
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/countdownlatch /CountDownLatchEx.java)
505
504
506
505
## CyclicBarrier
507
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/cyclicBarrierEx /CyclicBarrierEx.java)
506
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/cyclicbarrier /CyclicBarrierEx.java)
508
507
509
508
510
509
@@ -581,7 +580,7 @@ CopyOnWriteArraySet、ConcurrentSkipListSet
581
580
# 11. 原子类
582
581
583
582
* 无锁方案实现原理(Compare And Swap)
584
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/atomic/SimulatedCAS .java)
583
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/ atomic/SimulatedCompareAndSwap .java)
585
584
* 概览图
586
585

587
586
@@ -640,7 +639,7 @@ accumulateAndGet(x,func)
640
639
* 而创建一个线程,却需要调用操作系统内核的 API,然后操作系统要为线程分配一系列的资源,这个成本就很高了。
641
640
642
641
## 线程池是一种生产者-消费者模式(非一般意义池化资源)
643
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/threadPool/MyThreadPool.java)
642
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/ threadPool/MyThreadPool.java)
644
643
* Java ThreadPoolExecutor
645
644
646
645
```
@@ -713,7 +712,7 @@ get(long timeout, TimeUnit unit);
713
712
714
713
715
714
## FutureTask工具类(实现了RunnableFuture而它继承了Runnable和Future接口)
716
- * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/futureTask /FutureTaskEx.java)
715
+ * [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/features/futuretask /FutureTaskEx.java)
717
716
* 构造函数类似线程池submit
718
717
719
718
```
0 commit comments