多線程下的單例設計模式

來源:互聯網
上載者:User
我亦無他,唯手熟爾!多線程下的單例設計模式

眾所周知,單例模式中,構造方法是私人化的,通過靜態方法內部調用建構函式返回該類的執行個體對象。常見的代碼如下所示:

?
12345678910 public
class
Singleton {    private
static Singleton singletonObj; 
    private
Singleton(){}       public
static Singleton getInstance(){        if(singletonObj ==
null){            singletonObj =
new Singleton();        }        return
singletonObj;    }}

  

在單線程的情況下,確實可以保證只有一個執行個體,但是在多線程的情況下,就會發生意想不到的情況。

建立一個TestSingleton類,如下:

?
public
class
TestSingleton implements
Runnable {     private
Singleton s = null;         public
Singleton getS() {        return
s;    }     public
void setS(Singleton s) {        this.s = s;    }     public
static void
main(String[] args) {
        TestSingleton ts1 =
new TestSingleton();        TestSingleton ts2 =
new TestSingleton();        Thread t1 =
new Thread(ts1);        Thread t2 =
new Thread(ts2);        t1.start();        t2.start();                 Singleton s1 = ts1.getS();        Singleton s2 = ts2.getS();                 System.out.println(s1 == s2);    }     @Override    public
void run() {                 s = Singleton.getInstance();    }}

  

運行結果返回 false 

在多線程的環境中需要考慮同步的問題,對上述單例模式的代碼進行修改,如下:

?
public
static
Singleton getInstance() {         if
(singletonObj == null) {            synchronized
(Singleton.class) {                if
(singletonObj == null) {                    singletonObj =
new Singleton();                }            }        }        return
singletonObj;    }

  

重新運行上述TestSingleton ,返回結果 true 此時,保證即使是在多線程的環境下,依然能夠保持單例模式的正確性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.