Java單例模式實(shí)現(xiàn)的幾種方式
Java單例模式實(shí)現(xià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測試代碼:
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)文章
Spring數(shù)據(jù)庫連接池實(shí)現(xiàn)原理深入刨析
開發(fā)web項(xiàng)目,我們肯定會(huì)和數(shù)據(jù)庫打交道,因此就會(huì)涉及到數(shù)據(jù)庫鏈接的問題。在以前我們開發(fā)傳統(tǒng)的SSM結(jié)構(gòu)的項(xiàng)目時(shí)進(jìn)行數(shù)據(jù)庫鏈接都是通過JDBC進(jìn)行數(shù)據(jù)鏈接,我們每和數(shù)據(jù)庫打一次交道都需要先獲取一次鏈接,操作完后再關(guān)閉鏈接,這樣子效率很低,因此就出現(xiàn)了連接池2022-11-11
詳解spring cloud Feign使用中遇到的問題總結(jié)
本篇文章主要介紹了詳解spring cloud Feign使用中遇到的問題總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
深入理解Java8新特性之Stream API的創(chuàng)建方式和中間操作步驟
Stream是Java8的一大亮點(diǎn),是對(duì)容器對(duì)象功能的增強(qiáng),它專注于對(duì)容器對(duì)象進(jìn)行各種非常便利、高效的 聚合操作(aggregate operation)或者大批量數(shù)據(jù)操作。Stream API借助于同樣新出現(xiàn)的Lambda表達(dá)式,極大的提高編程效率和程序可讀性,感興趣的朋友快來看看吧2021-11-11
Spring動(dòng)態(tài)代理實(shí)現(xiàn)日志功能詳解
這篇文章主要為大家詳細(xì)介紹了Spring動(dòng)態(tài)代理實(shí)現(xiàn)日志功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
java實(shí)用驗(yàn)證碼的實(shí)現(xiàn)代碼
這篇文章主要為大家介紹了java實(shí)用驗(yàn)證碼的實(shí)現(xiàn)代碼,驗(yàn)證碼實(shí)際上就是隨機(jī)選擇一些字符以圖片的形式展現(xiàn)在頁面上,感興趣的小伙伴們可以參考一下2016-03-03
Java向上轉(zhuǎn)型和向下轉(zhuǎn)型實(shí)例解析
這篇文章主要介紹了Java向上轉(zhuǎn)型和向下轉(zhuǎn)型實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

