Skip to content

Commit a7eb966

Browse files
authored
Update 05_Collections_and_Thread_Safety.md
1 parent f7d1078 commit a7eb966

File tree

1 file changed

+7
-36
lines changed

1 file changed

+7
-36
lines changed

Diff for: ch11/05_Collections_and_Thread_Safety.md

+7-36
Original file line numberDiff line numberDiff line change
@@ -57,42 +57,13 @@
5757
的一般理论的完整处理,请参阅 `Doug Lea``Addison-Wesley`)的 `Java` 中的并发编程,以及有关 `Java` 中的并发性和集合实现的详细信息,请参阅
5858
`Brian Goetz` 等人的中的 **Java并发实践**。(`Addison-Wesley` 出版社)。
5959

60+
#### 同步和旧版集合
6061

62+
类似于 `ArrayStack` 的代码不是线程安全的,它由单线程执行,但可能在多线程环境中中断。 由于我们观察到的错误行为涉及两个线程同时执行 `push` 方法,因此
63+
我们可以更改程序以使其不可能。 使用 `synchronized` 来修改 `push` 方法的声明将保证一旦一个线程开始执行它,所有其他线程将被排除在该方法之外直到执行完
64+
成:
6165

62-
63-
64-
65-
66-
67-
68-
69-
70-
71-
72-
73-
74-
75-
76-
77-
78-
79-
80-
81-
82-
83-
84-
85-
86-
87-
88-
89-
90-
91-
92-
93-
94-
95-
96-
97-
66+
```java
67+
public synchronized void push(int elt) { ... }
68+
```
9869

0 commit comments

Comments
 (0)