作者coolcomm (coolcomm)
看板Programming
標題Fw: [問題] 想請問一段多執行緒的程式碼
時間Tue Feb 5 19:37:08 2013
※ [本文轉錄自 java 看板 #1H0J_st5 ]
作者: coolcomm (coolcomm) 看板: java
標題: [問題] 想請問一段多執行緒的程式碼
時間: Thu Jan 24 22:06:44 2013
以下程式碼節錄自org.apache.commons.lang3.builder.HashCodeBuilder
private static final ThreadLocal<Set<IDKey>> REGISTRY = new ThreadLocal<>();
static void register(Object value) {
synchronized(HashCodeBuilder.class) {
if (getRegistry() == null) { REGISTRY.set(new HashSet<IDKey>()); }
}
getRegistry().add(new IDKey(value));
}
想請問
ThreadLocal不是thread-safe嗎 為甚麼還要加上synchronized關鍵字?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.163.82.56
另外 synchronized(HashCodeBuilder.class) 有什麼特別含意嗎
為什麼不直接寫 synchronized(this)...
※ 編輯: coolcomm 來自: 1.163.82.56 (01/24 22:12)
1F:→ tkcn:因為 register 是 static method,沒有 this 01/24 22:24
3F:→ coolcomm:所以和static void synchronized register完全一樣囉? 01/24 22:29
4F:→ tkcn:是,但你上面的例子還有個 getRegistry 在外面,所以不同 01/24 23:16
5F:→ coolcomm:嗯 這部分了解了 01/24 23:21
※ 編輯: coolcomm 來自: 1.163.82.56 (01/24 23:24)
6F:→ n3oanderson:This is check then act scenario, which is not 01/26 15:37
7F:→ n3oanderson:thred safe. 01/26 15:39
囧 我發現我忘了貼getRegistry()的定義
static Set<IDKey> getRegistry() { return REGISTRY.get(); }
就這樣一行
※ 編輯: coolcomm 來自: 36.224.126.155 (02/05 19:37)
8F:→ coolcomm:有人能提供其他答案嗎? 36.224.126.155 02/05 19:37
9F:推 kingstong:所以說你看這種程式碼不會看到起瘋嗎?! 1.170.140.103 02/06 15:07
10F:推 kingstong:要是跑不動要怎麼DEBUG?!我可能會把電腦 1.170.140.103 02/06 15:10
11F:→ kingstong:砸了,反正編譯器那麼多種不必要一定得用 1.170.140.103 02/06 15:11
12F:→ kingstong:哪一種 1.170.140.103 02/06 15:11
13F:→ coolcomm:樓上又在幹嘛啦= = 1.163.86.92 02/06 18:15
14F:→ coolcomm:我猜樓上應該是不懂物件導向吧 所以才會 1.163.86.92 02/06 18:48
15F:→ coolcomm:覺得Java語法難懂 1.163.86.92 02/06 18:48
16F:→ coolcomm:雖然Java語法出了名的囉嗦 但其實非常好讀 1.163.86.92 02/06 18:50
17F:→ azureblaze:c大你跟他認真了你輸了 1.171.51.129 02/06 18:54
18F:→ coolcomm:= = 1.163.86.92 02/06 19:00
19F:推 kingstong:C\C++不就最早的物件導向的始祖嗎?! 1.170.140.103 02/06 20:15
20F:→ kingstong:不過我承認我位夠班,不過學了一堆理論 1.170.140.103 02/06 20:16
21F:→ kingstong:卻不會任何一種程式語言也是沒用 1.170.140.103 02/06 20:17
22F:→ kingstong:就像台大哲學和交大資工,你會選哪一個?! 1.170.140.103 02/06 20:19
23F:推 kingstong:還有你們認識施振榮和施崇棠嗎?!你們比 1.170.140.103 02/06 20:22
24F:→ kingstong:他們強嗎?! 1.170.140.103 02/06 20:23
※ 編輯: coolcomm 來自: 1.163.86.92 (02/06 20:46)
25F:推 LaPass:看不懂K的重點是什麼.....111.252.124.183 02/06 21:51
26F:→ vocaloid:視為雜訊略過 180.176.98.183 02/06 22:01
27F:推 dryman:C是物件導向的始祖?我笑了XD 68.181.5.30 02/07 13:04
28F:推 kingstong:樓上,你那麼厲害的話麻煩用一台沒灌任何 111.246.51.197 02/07 15:35
29F:→ kingstong:作業系統的電腦,然後灌DOS,在DOS的環境 111.246.51.197 02/07 15:36
30F:→ kingstong:下寫一套倚天中文系統的ET系列的可顯示 111.246.51.197 02/07 15:38
31F:→ kingstong:中文字的系統,在寫CW系列的文書處理程式 111.246.51.197 02/07 15:39
32F:→ kingstong:或是PE2也行,還是用COBOL來寫商用文書處 111.246.51.197 02/07 15:40
33F:→ kingstong:理系統,如果妳會我就讓你捅屁眼 111.246.51.197 02/07 15:41
34F:推 purpose:C++ 的物件導向是從 Simula 語言來的 124.8.134.217 02/07 16:06
35F:→ azureblaze:真正的高手灌什麼dos 用蝴蝶就夠了 1.171.48.45 02/07 16:12
36F:→ coolcomm:先別管king了 有沒有人能回答我原本的問題啦=3= 02/07 16:29
※ 編輯: coolcomm 來自: 1.163.82.9 (02/07 16:31)
37F:推 kingstong:PURPOSE你很謙虛啊!給你一個推 36.233.230.174 02/07 16:33
38F:→ maerdimer:屁精請不要亂入 111.252.70.168 02/07 21:18
39F:推 kingstong:我的屁比你的嘴還香勒 1.170.144.19 02/07 22:11
40F:推 kingstong:我知道我才疏學淺,不過我的精神就是不屈 1.170.144.19 02/07 22:18
41F:→ kingstong:不饒,我看過版主的發言,這個版想必在成 1.170.144.19 02/07 22:19
42F:→ kingstong:立時砲火連連,就像是宇宙大爆炸,混屯初 1.170.144.19 02/07 22:20
43F:→ kingstong:開,然後應該有人被桶進精神病院 1.170.144.19 02/07 22:21
44F:→ kingstong:不過程式這東西還是化繁為簡的好 1.170.144.19 02/07 22:21
45F:→ kingstong:不知道這版有沒有人問候別人的老木?! 1.170.144.19 02/07 22:23
46F:→ suhorng:如果你是來跟別人討論而不是亂放話混亂版 118.166.45.233 02/07 22:39
47F:→ suhorng:面就會有人討論了啊@@ 118.166.45.233 02/07 22:39
48F:推 dryman:請把不屈不撓的精神拿去讀點書,加油,好嗎 68.181.5.30 02/08 07:04
49F:→ yauhh:不屈不撓精神放在嘴上,不太好.歇會兒吧 118.167.53.24 02/08 16:49