8000 fix chapter1 ExampleCode & Test · funnycoding/java_learning@cb165b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb165b6

Browse files
committed
fix chapter1 ExampleCode & Test
add some pic
1 parent 1ac417f commit cb165b6

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

java_concurrency_in_practice/docs/2/2.线程安全性(总).md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ public class UnsafeCachingFactorizer implements Servlet {
173173

174174

175175

176-
177-
178176
### 活跃性与性能
179177

180178
锁的引入保证了线程安全性,但是同时也带来了活跃性与性能问题。如果没有分析好作用域而滥用锁的话,会导致程序非常严重的串行化,从而导致性能下降。
@@ -191,6 +189,22 @@ public class UnsafeCachingFactorizer implements Servlet {
191189

192190

193191

192+
193+
194+
### 总结
195+
196+
第一遍看的时候真是感觉信息量好大,这学了一圈再加上三刷、四刷之后发现不断压缩本章的核心内容的话,压缩之后的话这一章的内容也就还好,更多的也是偏概念上的,还没有真正开始深入讲 Java 中对应的实现,或者只是提了个概念。
197+
198+
同时我对**极客时间的《Java 并发编程实战》**这门课的内容的**感官也在不断的变化**
199+
200+
最开始只是觉得是这本书的下位替代,但是现在觉得也并不尽然。
201+
202+
其中包含了很多易懂的示例图,给出的代码也比书中仅是片段的要好一些,最重要的是深挖到了比较通用的并发编程解决范式比如管程模型 & 信号量,再到管程中的几个模型具体的介绍,哪怕只是个引子,也能让你明白该往什么方向去探究。
203+
204+
总的来说还是挺有价值的。
205+
206+
207+
194208
### 脑图:
195209

196210
<p align="center">
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package chapter1;
2+
3+
/**
4+
* @author XuYanXin
5+
* @program java_learning
6+
* @description
7+
* @date 2020/9/26 11:29 下午
8+
*/
9+
10+
public class NonSafeSequence {
11+
private int value;
12+
13+
public void addValue() throws InterruptedException {
14+
System.out.println("Thread:" + Thread.currentThread().getName()+"," + "value add 之前的值:" + value);
15+
value++;
16+
Thread.sleep(500);
17+
System.out.println("Thread:" + Thread.currentThread().getName()+"," + "value add 之后的值:" + value);
18+
}
19+
20+
pu 8000 blic int getValue() {
21+
return value;
22+
}
23+
}

java_concurrency_in_practice/src/main/java/chapter1/SafeSequence.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ public class SafeSequence {
1313
/**
1414
* 使用 synchronized Java的内置同步原语 —— 锁 就可以保证同一时间只有1个线程进入这个方法
1515
*/
16-
public synchronized void addValue() {
16+
public synchronized void addValue() throws InterruptedException {
17+
System.out.println("Thread:" + Thread.currentThread().getName()+"," + "value add 之前的值:" + value);
1718
value++;
19+
Thread.sleep(500);
20+
System.out.println("Thread:" + Thread.currentThread().getName()+"," + "value add 之后的值:" + value);
1821
}
1922

2023
public int getValue() {
Lines changed: 52 additions & 0 deletions
< 9E88 td data-grid-cell-id="diff-4f6bb5b34b9da797005e222b9c513aaf603330ffaf6961417d29e4b8343e06b6-empty-40-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package chapger1;
2+
3+
import chapter1.NonSafeSequence;
4+
import chapter1.SafeSequence;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
/**
9+
* @author XuYanXin
10+
* @program java_learning
11+
* @description
12+
* @date 2020/9/26 11:29 下午
13+
*/
14+
15+
public class Chapter1Test {
16+
@Test
17+
public void testUnsafeSequence() throws InterruptedException {
18+
NonSafeSequence nonSafeSequence = new NonSafeSequence();
19+
20+
for (int i = 0; i < 10; i++) {
21+
Thread thread = new Thread(() -> {
22+
try {
23+
nonSafeSequence.addValue();
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
});
28+
thread.start();
29+
}
30+
31+
Assert.assertEquals(10, nonSafeSequence.getValue());
32+
}
33+
34+
35+
@Test
36+
public void testSafeSequence() throws InterruptedException {
37+
SafeSequence ss = new SafeSequence();
38+
39+
for (int i = 0; i < 10; i++) {
40+
Thread thread = new Thread(() -> {
41+
try {
42+
ss.addValue();
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
}
46+
});
47+
thread.start();
48+
thread.join();
49+
}
50+
Assert.assertEquals(10, ss.getValue());
51+
}
52+
}

java_concurrency_in_practice/src/test/java/chapger1/UnsafeCountingFactorizerTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ public void testSafeSequence() throws InterruptedException {
3535
SafeSequence ss = new SafeSequence();
3636

3737
for (int i = 0; i < 10; i++) {
38-
Thread thread = new Thread(ss::addValue);
38+
Thread thread = new Thread(() -> {
39+
try {
40+
ss.addValue();
41+
} catch (InterruptedException e) {
42+
e.printStackTrace();
43+
}
44+
});
3945
thread.start();
4046
thread.join();
4147
}

0 commit comments

Comments
 (0)
0