Skip to content

Commit 7fd0b01

Browse files
committed
Update volatile.md
1 parent e991005 commit 7fd0b01

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

docs/thread/volatile.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ public static Penguin getInstance() {
308308

309309
这样就会导致线程 B 拿到一个不完整的 Penguin 对象,可能会出现空指针异常或者其他问题。
310310

311+
于是,我们可以为 m_penguin 变量添加 volatile 关键字,来禁止指令重排序,确保对象的初始化完成后再将其赋值给 m_penguin。
312+
311313
## 小结
312314

313315
“好了,三妹,我们来总结一下。”我舒了一口气说。

0 commit comments

Comments
 (0)