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

Java 單例模式的實(shí)現(xiàn)資料整理

 更新時(shí)間:2016年10月30日 14:53:30   作者:超超boy  
這篇文章主要介紹了Java 單例模式的實(shí)現(xiàn)的相關(guān)資料,并附簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下

Java單例模式的實(shí)現(xiàn),對(duì)java 單例模式的幾種實(shí)現(xiàn)方法進(jìn)行了整理:

單例模式好多書上都是這么寫的:

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

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

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

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

package singleton;
 
public class SingleTon1 {
   
   
  private SingleTon1(){
     
  }
 
  private static SingleTon1 instance = null;
   
  //多線程問題解法一,但是效率不高!因?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)建的類
  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ì)本站的支持!

相關(guān)文章

  • Java 多線程使用要點(diǎn)分析

    Java 多線程使用要點(diǎn)分析

    這篇文章主要介紹了Java 多線程使用要點(diǎn)分析的相關(guān)資料,Java 多線程開發(fā)需要主要的細(xì)節(jié)問題很多,這里就說下,需要的朋友可以參考下
    2016-12-12
  • Springboot注解之@EnableAutoConfiguration詳解

    Springboot注解之@EnableAutoConfiguration詳解

    這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個(gè)加載Starter目錄包之外的需要Spring自動(dòng)生成bean對(duì)象,本文對(duì)其進(jìn)行總結(jié),需要的朋友可以參考下
    2023-08-08
  • 詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用

    詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用

    這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法

    Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法

    這篇文章主要介紹了Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • 從面試中的問題分析ThreadLocal

    從面試中的問題分析ThreadLocal

    這篇文章主要介紹了從面試中的問題分析ThreadLocal,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來一起學(xué)習(xí)一下吧
    2019-06-06
  • Java中的@Conditional條件注解詳細(xì)解析

    Java中的@Conditional條件注解詳細(xì)解析

    這篇文章主要介紹了Java中的@Conditional條件注解詳細(xì)解析,@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊(cè)bean,需要的朋友可以參考下
    2023-11-11
  • MapTask工作機(jī)制圖文詳解

    MapTask工作機(jī)制圖文詳解

    今天小編就為大家分享一篇關(guān)于MapTask工作機(jī)制圖文詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java教程之引用類型數(shù)組和繼承的意義詳解

    Java教程之引用類型數(shù)組和繼承的意義詳解

    這篇文章主要介紹了Java教程之引用類型數(shù)組和繼承的意義詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出

    使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出

    這篇文章主要介紹了使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出,EasyExcel是一個(gè)快速解決大文件內(nèi)存溢出的Excel處理工具,它能讓你在不用考慮性能、內(nèi)存等因素的情況下,快速完成Excel的讀、寫等功能,需要的朋友可以參考下
    2023-10-10
  • 淺談Java中ArrayList的擴(kuò)容機(jī)制

    淺談Java中ArrayList的擴(kuò)容機(jī)制

    本文主要介紹了淺談Java中ArrayList的擴(kuò)容機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06

最新評(píng)論