發表文章

目前顯示的是 2月, 2016的文章

為什麼 abstract method 不能用 synchronized ?

        因為 synchronized method 取得的鎖是該 class 的 instance。class 有 abstract method,則 class 也要宣告為 abstract,而 abstract class 是不能被實例化的,因此就不能有 abstract synchronized method。         另一種說法是一個 method 是否為 synchronized 是看 method 的內容,也就是 method 的 implementation detail,而 abstract method 只定義了 interface,並沒有實做,自然也就不能用 synchronized。可參考  http://stackoverflow.com/questions/12805698/why-cant-an-abstract-method-be-synchronized         個人比較傾向第二種說法,因為 abstract class 裡其他有 implementation 的 method 還是能用 this,第一種說法比較不通。