Design Patterns 烏蒙山連著山外山---單件模式singleton pattern

來源:互聯網
上載者:User

標籤:des   style   blog   color   div   new   log   對象   

 1     //包含單件執行個體的類Singleton 2     public class Singleton 3     { 4         //聲明用於儲存單件執行個體的變數instance 5         private static Singleton instance; 6         //定義用於身分識別同步處理線程的對象locker 7         private static Object locker = new Object(); 8         //私人的建構函式Singleton 9         private Singleton() { }10         //公用訪問的返回單件執行個體的函數GetInstance11         public static Singleton GetInstance()12         {13             //第一重“鎖”只為了提高些效能14             if (instance == null)15             {16                 //線程鎖定塊17                 lock (locker)18                 {19                     //第二重“鎖”防止多線程多次new執行個體20                     if (instance == null)21                     {22                         //new唯一執行個體23                         instance = new Singleton();24                     }25                 }26             }27             //返回單件執行個體28             return instance;29         }30     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.