程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java單例模式實現的幾種方式,java例模式幾種

Java單例模式實現的幾種方式,java例模式幾種

編輯:JAVA綜合教程

Java單例模式實現的幾種方式,java例模式幾種


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

public class SingleTon1 {
	
	private SingleTon1(){	
	}

	private static SingleTon1 instance = null;

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

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

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

1.使用synchronized 關鍵字

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.加鎖

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.利用靜態變量:

package singleton;


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

以上就是常用的創建單例的模式:

Test測試代碼:

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved