欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java單例模式實(shí)現(xiàn)的幾種方式

 更新時(shí)間:2016年09月21日 14:05:45   作者:超超boy  
這篇文章主要介紹了Java單例模式實(shí)現(xiàn)的幾種方式的相關(guān)資料,需要的朋友可以參考下

Java單例模式實(shí)現(xiàn)的幾種方式

單例模式好多書(shū)上都是這么寫(xiě)的:

public class SingleTon1 {
   
  private SingleTon1(){ 
  }
 
  private static SingleTon1 instance = null;
 
   
  public static SingleTon1 getInstance(){
    if(instance == null){
      instance = new SingleTon1();
    }
    return instance;
  }
}

  但是實(shí)際開(kāi)發(fā)中是不會(huì)這么寫(xiě)的,因?yàn)橛幸粋€(gè)嚴(yán)重的問(wèn)題,多線程并發(fā)訪問(wèn)的時(shí)候,可能會(huì)產(chǎn)生多個(gè)實(shí)例??!

下面列舉幾個(gè)常用的方法:

1.使用synchronized 關(guān)鍵字

package singleton;
 
public class SingleTon1 {
   
   
  private SingleTon1(){
     
  }
 
  private static SingleTon1 instance = null;
   
  //多線程問(wèn)題解法一,但是效率不高!因?yàn)槊看握{(diào)用都會(huì)加鎖!
  public static synchronized SingleTon1 getInstance(){
    if(instance == null){
      instance = new SingleTon1();
    }
    return instance;
  }
  public void print(){
    System.out.println("thread_id:"+Thread.currentThread().getId());
  }
   
  private static Object object = new Object();
  //很巧妙的方法,只有在null的時(shí)候加鎖,之后就不加啦
  public static SingleTon1 getInstance2(){
     
    if(instance == null){
      synchronized (object){
        instance = new SingleTon1();
      }
    }
    return instance;
  }
 
}

 2.加鎖

package singleton;
 
import java.util.concurrent.locks.ReentrantLock;
 
public class SingleTon2 {
   
  private SingleTon2(){
     
  }
  private static ReentrantLock lock = new ReentrantLock();
  private static SingleTon2 instance = null;
   
   
  public void print(){
    System.out.println("thread_id:"+Thread.currentThread().getId());
  }
   
  public static SingleTon2 getInstance2(){
     
    if(instance == null){
      lock.lock();
      if(instance == null){ //注意這里還要判斷下?。?
        instance = new SingleTon2();
      }
      lock.unlock();
    }
    return instance;
  }
}

  3.利用靜態(tài)變量:

package singleton;
 
 
public class SingleTon3 {
   
  public static void print(){
    System.out.println("thread_id:"+Thread.currentThread().getId());
  }
   
  public static Nested getNested(){
   
    return Nested.instance;
  }
  //這個(gè)是單例創(chuàng)建的類(lèi)
  static class Nested{
   private Nested(){
    }
  static Nested instance = new Nested();
  }
}

以上就是常用的創(chuàng)建單例的模式:

Test測(cè)試代碼:

package singleton;
 
import singleton.SingleTon3.Nested;
 
public class Test2 {
 
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Nested singleton;
    Myrunnable mm = new Myrunnable();
    Myrunnable m1 = new Myrunnable();
     
    Myrunnable2 m2 = new Myrunnable2();
    new Thread(m1).start();
    new Thread(m2).start();
    if(m1.singleton == m2.singleton){ //是同一個(gè)
      System.out.println("是同一個(gè)");
    }else{
      System.out.println("不是同一個(gè)");
    }
   }
}
  class Myrunnable implements Runnable{
    Nested singleton;
      @Override
      public void run() {
        // TODO Auto-generated method stub
        singleton = SingleTon3.getNested();
        SingleTon3.print();
      }
  }
   
  class Myrunnable2 implements Runnable{
    Nested singleton;
    @Override
    public void run() {
      // TODO Auto-generated method stub
      singleton = SingleTon3.getNested();
      SingleTon3.print();
    }
  }

輸出:

是同一個(gè)

thread_id:11
thread_id:10

以上就是對(duì)Java 單例模式的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 淺談springboot項(xiàng)目中定時(shí)任務(wù)如何優(yōu)雅退出

    淺談springboot項(xiàng)目中定時(shí)任務(wù)如何優(yōu)雅退出

    這篇文章主要介紹了淺談springboot項(xiàng)目中定時(shí)任務(wù)如何優(yōu)雅退出?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • 淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated)

    淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated)

    這篇文章主要介紹了淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 聊聊Springboot2.x的session和cookie有效期

    聊聊Springboot2.x的session和cookie有效期

    這篇文章主要介紹了Springboot2.x的session和cookie有效期,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot配置嵌入式servlet容器的方法

    springboot配置嵌入式servlet容器的方法

    這篇文章主要介紹了springboot配置嵌入式servlet容器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Mybatis?Plus?新版lambda?表達(dá)式查詢異常的處理

    Mybatis?Plus?新版lambda?表達(dá)式查詢異常的處理

    這篇文章主要介紹了Mybatis?Plus?新版lambda?表達(dá)式查詢異常的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • JAVA使用POI獲取Excel的列數(shù)與行數(shù)

    JAVA使用POI獲取Excel的列數(shù)與行數(shù)

    Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java程式對(duì)Microsoft Office格式檔案讀和寫(xiě)的功能。 下面這篇文章給大家介紹了JAVA使用POI獲取Excel列數(shù)和行數(shù)的方法,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2016-12-12
  • 快速排序的深入詳解以及java實(shí)現(xiàn)

    快速排序的深入詳解以及java實(shí)現(xiàn)

    本篇文章是對(duì)java中的快速排序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • Spring定時(shí)任務(wù)@scheduled多線程使用@Async注解示例

    Spring定時(shí)任務(wù)@scheduled多線程使用@Async注解示例

    這篇文章主要為大家介紹了Spring定時(shí)任務(wù)@scheduled多線程使用@Async注解示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • SpringBoot淺析緩存機(jī)制之Ehcache?2.x應(yīng)用

    SpringBoot淺析緩存機(jī)制之Ehcache?2.x應(yīng)用

    EhCache?是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn)。它是Hibernate中的默認(rèn)緩存框架。Ehcache已經(jīng)發(fā)布了3.1版本。但是本文的講解基于2.x版本
    2022-08-08
  • 探討Java中函數(shù)是值傳遞還是引用傳遞問(wèn)題

    探討Java中函數(shù)是值傳遞還是引用傳遞問(wèn)題

    這篇文章主要介紹了探討Java中函數(shù)是值傳遞還是引用傳遞問(wèn)題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02

最新評(píng)論