Skip to content

Commit 9966519

Browse files
committed
readme and lock test
1 parent b89a3d7 commit 9966519

File tree

6 files changed

+438
-7
lines changed

6 files changed

+438
-7
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
内容整理自[《Java并发编程实战》](https://time.geekbang.org/column/intro/159)
22
相关工具类[《vjtools》](https://github.com/vipshop/vjtools)
33

4+
45
# 1.序言及全览
56
## 学习并发的原因
67
* 硬件驱动
@@ -151,8 +152,10 @@ public class Singleton {
151152
## final
152153
153154
* [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/finalEx/FinalExample.java)
155+
* 修饰变量时,初衷是告诉编译器:这个变量生而不变,非immutable,即只能表示对象引用不能被赋值(例如List);
156+
* 修饰方法则方法不能被重写
157+
* 修饰类则不能被扩展继承。
154158
155-
`修饰变量时,初衷是告诉编译器:这个变量生而不变。`
156159
157160
158161
```
@@ -300,8 +303,7 @@ public class Singleton {
300303
* 多核CPU :最佳线程数 =CPU 核数 * [ 1 +(I/O 耗时 / CPU 耗时)
301304
302305
-------
303-
304-
# 6. 若干反例
306+
#6. 若干反例
305307
306308
307309
```
@@ -432,6 +434,7 @@ public ReentrantLock(boolean fair){
432434
* 永远只在访问可变的成员变量时加锁
433435
* 永远不在调用其他对象的方法时加锁
434436
437+
435438
-------
436439
# 8.Semaphore
437440
### 信号量模型
@@ -476,6 +479,7 @@ class Semaphore{
476479
#### 实现限流器(Semaphore 可以允许多个线程访问一个临界区)
477480
* [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/semaphore/SemaphoreEx.java)
478481
482+
479483
-------
480484
# 9.ReadWriteLock、StampedLock、CountDownLatch、CyclicBarrier
481485
@@ -496,6 +500,7 @@ class Semaphore{
496500
* [代码示例](https://github.com/Fadezed/concurrency/blob/master/src/main/java/com/example/concurrency/cyclicBarrierEx/CyclicBarrierEx.java)
497501
498502
503+
499504
-------
500505
# 10. 并发容器
501506
## 同步容器(jdk1.5 之前)
@@ -564,8 +569,8 @@ CopyOnWriteArraySet、ConcurrentSkipListSet
564569
* 双端非阻塞队列
565570
* ConcurrentLinkedDeque
566571
567-
-------
568572
573+
-------
569574
# 11. 原子类
570575
571576
* 无锁方案实现原理(Compare And Swap)
@@ -613,14 +618,14 @@ accumulateAndGet(x,func)
613618
* AtomicLongFieldUpdater
614619
* AtomicReferenceFieldUpdater
615620
616-
## 原子化累加器(只支持累加操作性能比原子化基本数据类型更好,不支持compareAndSet())
621+
## 原子化累加器(空间换时间,只支持累加操作性能比原子化基本数据类型更好,不支持compareAndSet())
617622
* DoubleAccumulator
618623
* DoubleAdder
619624
* LongAccumulator
620625
* LongAdder
621626
622-
623627
-------
628+
624629
# 12. 线程池
625630
626631
## 为什么要用线程池
@@ -660,6 +665,7 @@ ThreadPoolExecutor(
660665
* 慎用默认拒绝策略RejectedExecutionException不强制处理容易忽略,建议自定义拒绝策略配合策略降级使用
661666
* 异常处理不会通知所有需要按需捕获处理异常
662667
668+
663669
-------
664670
# 13. Future
665671
## 获得任务执行结果
@@ -708,4 +714,3 @@ FutureTask(Callable<V> callable);
708714
FutureTask(Runnable runnable, V result);
709715

710716
```
711-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.example.concurrency.dflockTest;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
public class LockTest {
8+
private static int count;
9+
10+
private static int readThreadNum = 0;
11+
private static int writeThreadNum = 1;
12+
13+
private static int maxValue = 10000;
14+
private final Lock lock = new ReentrantLock();
15+
16+
public static void main(String[] args) {
17+
Counter lockTest = new LockTest().new Counter();
18+
long startTime = System.currentTimeMillis();
19+
CountDownLatch latch = new CountDownLatch(readThreadNum + writeThreadNum);
20+
for (int i = 0; i < writeThreadNum; i++) {
21+
new Thread(() -> {
22+
for (int cur = 0; cur < maxValue; cur++) {
23+
lockTest.count();
24+
}
25+
latch.countDown();
26+
}).start();
27+
}
28+
29+
for (int i = 0; i < readThreadNum; i++) {
30+
new Thread(() -> {
31+
for (int cur = 0; cur < maxValue; cur++) {
32+
lockTest.get();
33+
}
34+
latch.countDown();
35+
}).start();
36+
}
37+
try {
38+
latch.await();
39+
} catch (InterruptedException e) {
40+
// TODO Auto-generated catch block
41+
e.printStackTrace();
42+
}
43+
long endTime = System.currentTimeMillis();
44+
45+
System.out.println("LockTest执行时长:" + (endTime - startTime) + ", count" + count);
46+
47+
}
48+
49+
class Counter {
50+
51+
/**
52+
* 获取count值
53+
* @return
54+
* @throws InterruptedException
55+
*/
56+
public int get() {
57+
58+
lock.lock();
59+
try {
60+
try {
61+
Thread.sleep(1);
62+
} catch (InterruptedException e) {
63+
// TODO Auto-generated catch block
64+
e.printStackTrace();
65+
}
66+
return count;
67+
// System.out.println("count:" + count);
68+
} finally {
69+
lock.unlock();
70+
}
71+
}
72+
73+
/**
74+
* count值+1
75+
* @return
76+
* @throws InterruptedException
77+
*/
78+
public void count() {
79+
lock.lock();
80+
try {
81+
try {
82+
Thread.sleep(1);
83+
} catch (InterruptedException e) {
84+
// TODO Auto-generated catch block
85+
e.printStackTrace();
86+
}
87+
count++;
88+
// System.out.println("count:" + count);
89+
} finally {
90+
lock.unlock();
91+
}
92+
}
93+
}
94+
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.example.concurrency.dflockTest;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.atomic.LongAdder;
5+
6+
public class LongAdderTest {
7+
private static LongAdder count = new LongAdder();
8+
9+
10+
private static int readThreadNum = 10;
11+
private static int writeThreadNum = 0;
12+
13+
private static int maxValue = 1000;
14+
15+
public static void main(String[] args) {
16+
Counter lockTest = new LongAdderTest().new Counter();
17+
long startTime = System.currentTimeMillis();
18+
CountDownLatch latch = new CountDownLatch(readThreadNum + writeThreadNum);
19+
for (int i = 0; i < writeThreadNum; i++) {
20+
new Thread(() -> {
21+
for (int cur = 0; cur < maxValue; cur++) {
22+
lockTest.count();
23+
}
24+
latch.countDown();
25+
}).start();
26+
}
27+
28+
for (int i = 0; i < readThreadNum; i++) {
29+
new Thread(() -> {
30+
for (int cur = 0; cur < maxValue; cur++) {
31+
lockTest.get();
32+
}
33+
latch.countDown();
34+
}).start();
35+
}
36+
try {
37+
latch.await();
38+
} catch (InterruptedException e) {
39+
// TODO Auto-generated catch block
40+
e.printStackTrace();
41+
}
42+
long endTime = System.currentTimeMillis();
43+
44+
System.out.println("LockTest执行时长:" + (endTime - startTime) + ", count" + count);
45+
46+
}
47+
48+
class Counter {
49+
50+
51+
public long get() {
52+
try {
53+
Thread.sleep(1);
54+
} catch (InterruptedException e) {
55+
// TODO Auto-generated catch block
56+
e.printStackTrace();
57+
}
58+
return count.sum();
59+
}
60+
61+
public void count() {
62+
try {
63+
Thread.sleep(1);
64+
} catch (InterruptedException e) {
65+
// TODO Auto-generated catch block
66+
e.printStackTrace();
67+
}
68+
count.increment();
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.example.concurrency.dflockTest;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.CountDownLatch;
6+
import java.util.concurrent.locks.ReentrantReadWriteLock;
7+
8+
9+
public class RTTTest {
10+
private static int count;
11+
12+
private static int readThreadNum = 10;
13+
private static int writeThreadNum = 0;
14+
15+
private static int maxValue = 10000;
16+
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
17+
18+
public static void main(String[] args) {
19+
List list = new ArrayList();
20+
Counter lockTest = new RTTTest().new Counter();
21+
long startTime = System.currentTimeMillis();
22+
CountDownLatch latch = new CountDownLatch(readThreadNum + writeThreadNum);
23+
for (int i = 0; i < writeThreadNum; i++) {
24+
new Thread(() -> {
25+
for (int cur = 0; cur < maxValue; cur++) {
26+
lockTest.count();
27+
}
28+
latch.countDown();
29+
}).start();
30+
}
31+
32+
for (int i = 0; i < readThreadNum; i++) {
33+
new Thread(() -> {
34+
for (int cur = 0; cur < maxValue; cur++) {
35+
lockTest.get();
36+
}
37+
latch.countDown();
38+
}).start();
39+
}
40+
try {
41+
latch.await();
42+
} catch (InterruptedException e) {
43+
// TODO Auto-generated catch block
44+
e.printStackTrace();
45+
}
46+
long endTime = System.currentTimeMillis();
47+
48+
System.out.println("LockTest执行时长:" + (endTime - startTime) + ", count" + count);
49+
50+
}
51+
52+
class Counter {
53+
54+
public int get(){
55+
56+
lock.readLock().lock();
57+
try {
58+
try {
59+
Thread.sleep(1);
60+
} catch (InterruptedException e) {
61+
// TODO Auto-generated catch block
62+
e.printStackTrace();
63+
}
64+
return count;
65+
// System.out.println("count:" + count);
66+
} finally {
67+
lock.readLock().unlock();
68+
}
69+
}
70+
71+
72+
public void count() {
73+
lock.writeLock().lock();
74+
try {
75+
try {
76+
Thread.sleep(1);
77+
} catch (InterruptedException e) {
78+
// TODO Auto-generated catch block
79+
e.printStackTrace();
80+
}
81+
count++;
82+
// System.out.println("count:" + count);
83+
} finally {
84+
lock.writeLock().unlock();
85+
}
86+
}
87+
}
88+
89+
90+
}

0 commit comments

Comments
 (0)