一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java單例模式實現(xiàn)的幾種方式

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

2020-06-17 12:09超超boy JAVA教程

這篇文章主要介紹了Java單例模式實現(xiàn)的幾種方式的相關資料,需要的朋友可以參考下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class SingleTon1 {
   
  private SingleTon1(){
  }
 
  private static SingleTon1 instance = null;
 
   
  public static SingleTon1 getInstance(){
    if(instance == null){
      instance = new SingleTon1();
    }
    return instance;
  }
}

  但是實際開發(fā)中是不會這么寫的,因為有一個嚴重的問題,多線程并發(fā)訪問的時候,可能會產生多個實例!!

下面列舉幾個常用的方法:

1.使用synchronized 關鍵字

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package singleton;
 
public class SingleTon1 {
   
   
  private SingleTon1(){
     
  }
 
  private static SingleTon1 instance = null;
   
  //多線程問題解法一,但是效率不高!因為每次調用都會加鎖!
  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的時候加鎖,之后就不加啦
  public static SingleTon1 getInstance2(){
     
    if(instance == null){
      synchronized (object){
        instance = new SingleTon1();
      }
    }
    return instance;
  }
 
}

 2.加鎖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)變量:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package singleton;
 
 
public class SingleTon3 {
   
  public static void print(){
    System.out.println("thread_id:"+Thread.currentThread().getId());
  }
   
  public static Nested getNested(){
   
    return Nested.instance;
  }
  //這個是單例創(chuàng)建的類
  static class Nested{
   private Nested(){
    }
  static Nested instance = new Nested();
  }
}

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

Test測試代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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){ //是同一個
      System.out.println("是同一個");
    }else{
      System.out.println("不是同一個");
    }
   }
}
  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();
    }
  }

輸出:

是同一個

thread_id:11
thread_id:10

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

原文鏈接:http://www.cnblogs.com/jycboy/p/5892185.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91亚洲精品国产自在现线 | 日韩欧美中文字幕一区二区三区 | 欧美成人免费tv在线播放 | 国产女同精品 | 久久国产36精品色熟妇 | 92精品国产成人观看免费 | 小早川怜子在线播放精品 | 亚洲免费在线观看 | 好吊色永久免费视频大全 | 国产日本久久久久久久久婷婷 | 国产成人影院在线观看 | 免费标准高清看机机桶机机 | 日本激情在线 | 精品国语对白精品自拍视 | 亚洲va天堂va国产va久久 | yellow高清免费观看日本 | 日本成年片高清在线观看 | 扒开斗罗美女了的胸罩和内裤漫画 | 色花堂国产精品首页第一页 | 国产精彩对白综合视频 | 国产欧美日韩亚洲精品区2345 | 国内精品久久久久久野外 | 日韩网站免费 | 秋霞啪啪片| 三上悠亚久久国产 | 午夜伦理电影在线观免费 | 无限资源在线观看完整版免费下载 | 亚欧有色在线观看免费版高清 | 私人黄色影院 | 日本在线视频网址 | 法国老妇性xx在线播放 | 欧美男同video | 美女鸡 | 波多野结衣女老师 | 女毛片| 精品亚洲一区二区三区在线播放 | 国内精品视频免费观看 | zzjj中国| 841995论坛网站2022年 | a级影视 | 欧美作爱福利免费观看视频 |