Fw: [問題] 想請問一段多執行緒的程式碼
※ [本文轉錄自 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)
→
01/24 22:24, , 1F
01/24 22:24, 1F
→
01/24 22:26, , 2F
01/24 22:26, 2F
→
01/24 22:29, , 3F
01/24 22:29, 3F
→
01/24 23:16, , 4F
01/24 23:16, 4F
→
01/24 23:21, , 5F
01/24 23:21, 5F
※ 編輯: coolcomm 來自: 1.163.82.56 (01/24 23:24)
→
01/26 15:37, , 6F
01/26 15:37, 6F
→
01/26 15:39, , 7F
01/26 15:39, 7F
囧 我發現我忘了貼getRegistry()的定義
static Set<IDKey> getRegistry() { return REGISTRY.get(); }
就這樣一行
※ 編輯: coolcomm 來自: 36.224.126.155 (02/05 19:37)
→
02/05 19:37, , 8F
02/05 19:37, 8F
推
02/06 15:07, , 9F
02/06 15:07, 9F
推
02/06 15:10, , 10F
02/06 15:10, 10F
→
02/06 15:11, , 11F
02/06 15:11, 11F
→
02/06 15:11, , 12F
02/06 15:11, 12F
→
02/06 18:15, , 13F
02/06 18:15, 13F
→
02/06 18:48, , 14F
02/06 18:48, 14F
→
02/06 18:48, , 15F
02/06 18:48, 15F
→
02/06 18:50, , 16F
02/06 18:50, 16F
→
02/06 18:54, , 17F
02/06 18:54, 17F
→
02/06 19:00, , 18F
02/06 19:00, 18F
推
02/06 20:15, , 19F
02/06 20:15, 19F
→
02/06 20:16, , 20F
02/06 20:16, 20F
→
02/06 20:17, , 21F
02/06 20:17, 21F
→
02/06 20:19, , 22F
02/06 20:19, 22F
推
02/06 20:22, , 23F
02/06 20:22, 23F
→
02/06 20:23, , 24F
02/06 20:23, 24F
※ 編輯: coolcomm 來自: 1.163.86.92 (02/06 20:46)
推
02/06 21:51, , 25F
02/06 21:51, 25F
→
02/06 22:01, , 26F
02/06 22:01, 26F
推
02/07 13:04, , 27F
02/07 13:04, 27F
推
02/07 15:35, , 28F
02/07 15:35, 28F
→
02/07 15:36, , 29F
02/07 15:36, 29F
→
02/07 15:38, , 30F
02/07 15:38, 30F
→
02/07 15:39, , 31F
02/07 15:39, 31F
→
02/07 15:40, , 32F
02/07 15:40, 32F
→
02/07 15:41, , 33F
02/07 15:41, 33F
推
02/07 16:06, , 34F
02/07 16:06, 34F
→
02/07 16:12, , 35F
02/07 16:12, 35F
→
02/07 16:29, , 36F
02/07 16:29, 36F
※ 編輯: coolcomm 來自: 1.163.82.9 (02/07 16:31)
推
02/07 16:33, , 37F
02/07 16:33, 37F
→
02/07 21:18, , 38F
02/07 21:18, 38F
推
02/07 22:11, , 39F
02/07 22:11, 39F
推
02/07 22:18, , 40F
02/07 22:18, 40F
→
02/07 22:19, , 41F
02/07 22:19, 41F
→
02/07 22:20, , 42F
02/07 22:20, 42F
→
02/07 22:21, , 43F
02/07 22:21, 43F
→
02/07 22:21, , 44F
02/07 22:21, 44F
→
02/07 22:23, , 45F
02/07 22:23, 45F
→
02/07 22:39, , 46F
02/07 22:39, 46F
→
02/07 22:39, , 47F
02/07 22:39, 47F
推
02/08 07:04, , 48F
02/08 07:04, 48F
→
02/08 16:49, , 49F
02/08 16:49, 49F
Programming 近期熱門文章
PTT數位生活區 即時熱門文章