作者abschihok (大帥哥)
站內java
標題[問題] synchronized的問題
時間Tue Sep 15 23:27:56 2009
請問以下二組各有何不同?
第一組:
class Some{
public static synchronized void method(){
......
}
}
class Some{
public synchronized void method(){
......
}
}
以上差別在有無static字眼
第二組:
class Some{
void method(){
synchronized(Some.class){
....
}
}
}
class Some{
void method( ){
synchronized(this){
....
}
}
}
請問以上兩組各有何不同?
我的看法是:
第一組:如果有加static的話,代表可以鎖住不同的Some 物件的thread,
如果沒加static的話,只能鎖住本身的Some物件的thread
第二組:其實也是和第一組的一樣,如果是synchronized(Some.class){}的話,
代表可以鎖住不同Some物件的thread
請問大家,我的答案是對的嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.160.147.66
→ sbrhsieh:何謂【鎖住某物件的thread】?! 09/16 00:58
→ sbrhsieh:物件跟 Thread 有什麼關聯? 09/16 00:59
→ adrianshum:不對.什麼鎖住thread,鎖住不同Some物件通通亂來... =_= 09/16 01:00
→ jej:跑hashcode出來看看吧... 09/19 00:23