File tree Expand file tree Collapse file tree 5 files changed +102
-4
lines changed
java_concurrency_in_practice Expand file tree Collapse file tree 5 files changed +102
-4
lines changed Original file line number Diff line number Diff line change @@ -173,8 +173,6 @@ public class UnsafeCachingFactorizer implements Servlet {
173
173
174
174
175
175
176
-
177
-
178
176
### 活跃性与性能
179
177
180
178
锁的引入保证了线程安全性,但是同时也带来了活跃性与性能问题。如果没有分析好作用域而滥用锁的话,会导致程序非常严重的串行化,从而导致性能下降。
@@ -191,6 +189,22 @@ public class UnsafeCachingFactorizer implements Servlet {
191
189
192
190
193
191
192
+
193
+
194
+ ### 总结
195
+
196
+ 第一遍看的时候真是感觉信息量好大,这学了一圈再加上三刷、四刷之后发现不断压缩本章的核心内容的话,压缩之后的话这一章的内容也就还好,更多的也是偏概念上的,还没有真正开始深入讲 Java 中对应的实现,或者只是提了个概念。
197
+
198
+ 同时我对** 极客时间的《Java 并发编程实战》** 这门课的内容的** 感官也在不断的变化** 。
199
+
200
+ 最开始只是觉得是这本书的下位替代,但是现在觉得也并不尽然。
201
+
202
+ 其中包含了很多易懂的示例图,给出的代码也比书中仅是片段的要好一些,最重要的是深挖到了比较通用的并发编程解决范式比如管程模型 & 信号量,再到管程中的几个模型具体的介绍,哪怕只是个引子,也能让你明白该往什么方向去探究。
203
+
204
+ 总的来说还是挺有价值的。
205
+
206
+
207
+
194
208
### 脑图:
195
209
196
210
< p align= " center" >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -13,8 +13,11 @@ public class SafeSequence {
13
13
/**
14
14
* 使用 synchronized Java的内置同步原语 —— 锁 就可以保证同一时间只有1个线程进入这个方法
15
15
*/
16
- public synchronized void addValue () {
16
+ public synchronized void addValue () throws InterruptedException {
17
+ System .out .println ("Thread:" + Thread .currentThread ().getName ()+"," + "value add 之前的值:" + value );
17
18
value ++;
19
+ Thread .sleep (500 );
20
+ System .out .println ("Thread:" + Thread .currentThread ().getName ()+"," + "value add 之后的值:" + value );
18
21
}
19
22
20
23
public int getValue () {
Original file line number Diff line number Diff line change
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 ++) {
<
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">
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
+ }
Original file line number Diff line number Diff line change @@ -35,7 +35,13 @@ public void testSafeSequence() throws InterruptedException {
35
35
SafeSequence ss = new SafeSequence ();
36
36
37
37
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
+ });
39
45
thread .start ();
40
46
thread .join ();
41
47
}
You can’t perform that action at this time.
0 commit comments