JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Software Design » GoF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) [Re:popcorny]
shukae





發文: 70
積分: 0
於 2010-12-17 14:42 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
搜尋到這篇...
回應一下我的看法(這玩法我也常用, 而且更複雜)
用DCL的作法必然是不想要馬上建立該instance
又必須兼顧到效能與多執行緒問題
所以用兩個if一個sync來控制
但是當多CPU環境可能就會遇到先assignment後constructor的問題
還有JIT也會自己把程式碼最佳化.......
這問題我認為要解決也不是那麼難
我的構想是『呼叫method』,原因基於下列兩項假設
1. JIT compiler 不會把method合併
2. JIT compiler不會把看似沒用method呼叫刪除掉
所以
instance 建立的過程,呼叫一個method
在method 中將物件建立之後呼叫一個該物件的method
例如呼叫hashCode(), 能被呼叫必然已經完成物件的建立
之後才return並且assignment
這樣就可以解決這些問題

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TTT {
 
  private static TTT my;
 
  public static TTT getInstance(){
    if(my==null){
      synchronized (TTT.class) {
        if(my==null){
          my = create(); // 以呼叫method方式指派, 避免被最佳化時程式碼被合併
        }
      }
    }
    return my;
  }
 
  protected static TTT create(){
    TTT tmp = new TTT(); // 建立物件與下面的hashCode()/return有相依性, 不可能被CPU同時執行
    tmp.hashCode();  // 呼叫某個 Method, 以確保物件建構完成
    return tmp;
  }
 
}


shukae edited on 2011-01-24 12:58
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
31199 [精華] [DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 660 2003-07-19 16:13
29448 Re:[DP]Singleton, Double-check Locking(by Biologic popcorny 1727 2003-07-19 16:17
29178 Re:[DP]Singleton, Double-check Locking(by Biologic Biologic 102 2003-07-19 16:33
29353 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) ymshin 584 2003-07-20 04:01
29218 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) Biologic 1007 2003-07-20 09:47
28883 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) ymshin 591 2003-07-20 10:47
28880 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) Biologic 265 2003-07-20 12:47
28844 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) ymshin 350 2003-07-20 13:58
29658 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 3208 2003-07-20 13:49
29553 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 1942 2003-07-20 14:03
28944 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 266 2003-07-20 15:09
28981 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) ymshin 1054 2003-07-20 15:41
28777 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) browser 764 2003-07-20 16:01
28876 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) ymshin 183 2003-07-20 16:04
28948 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 1214 2003-07-20 16:18
28923 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) ymshin 1738 2003-07-20 17:00
29000 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) william 320 2003-08-03 02:36
29058 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 1843 2003-07-20 17:28
29076 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 1218 2003-07-20 18:20
28918 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 2402 2003-07-20 14:09
28952 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 684 2003-07-20 16:34
28898 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 333 2003-07-20 20:23
28793 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 571 2003-07-20 21:39
28868 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 250 2003-07-20 22:14
28764 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 380 2003-07-20 22:45
27911 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) plutotw 526 2004-09-02 09:47
28136 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) alien 2115 2004-09-03 09:01
28531 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) plutotw 725 2004-09-03 21:03
25689 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) zanyking 672 2005-08-24 13:43
12808 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) shukae 876 2010-12-17 14:42
» JWorld@TW »  Software Design » GoF

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8