天天看點

多線程程式設計學習(2)之single threaded execution pattern

Single Threaded Execution Pattern【獨木橋模式】

一:single threaded execution pattern的參與者

--->SharedResource(共享資源)

二:single threaded execution pattern模式什麼時候使用

--->多線程程式設計時

--->資料可被多個線程通路的時候

--->共享資源狀态可能變化的時候

--->需要確定資料安全性的時候

三:single threaded execution pattern思考

--->synchronized一見到它,勢必保護着什麼公共資源的資料。保證資料安全,就得所有該保護的地方都得保護。

--->保護公共資源的資料的範圍叫臨界區,臨界區盡可能的小。提高性能。

--->程式設計的時候,一定要防止死鎖的發生。主要是同步方法的外部調用順序,防止交叉調用,多線程時,會發生死鎖。

案例:三個人來回通過一扇門,通過時記錄該人的姓名和位址。

門類(公共資源)

多線程程式設計學習(2)之single threaded execution pattern
多線程程式設計學習(2)之single threaded execution pattern

View Code

人類(線程類)

多線程程式設計學習(2)之single threaded execution pattern
多線程程式設計學習(2)之single threaded execution pattern

測試類(主線程)

多線程程式設計學習(2)之single threaded execution pattern
多線程程式設計學習(2)之single threaded execution pattern

繼續閱讀