[問題] synchronized的問題
請問以下二組各有何不同?
第一組:
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
→
09/16 00:58, , 1F
09/16 00:58, 1F
→
09/16 00:59, , 2F
09/16 00:59, 2F
→
09/16 01:00, , 3F
09/16 01:00, 3F
→
09/19 00:23, , 4F
09/19 00:23, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):