Java 單例模式的實(shí)現(xiàn)資料整理
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)文章
Spring Security實(shí)現(xiàn)動(dòng)態(tài)路由權(quán)限控制方式
這篇文章主要介紹了Spring Security實(shí)現(xiàn)動(dòng)態(tài)路由權(quán)限控制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
java使用POI讀取properties文件并寫到Excel的方法
這篇文章主要介紹了java使用POI讀取properties文件并寫到Excel的方法,涉及java操作properties文件及Excel文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06
通過Spring Boot配置動(dòng)態(tài)數(shù)據(jù)源訪問多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了通過Spring Boot配置動(dòng)態(tài)數(shù)據(jù)源訪問多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-03-03
springboot 啟動(dòng)如何排除某些bean的注入
這篇文章主要介紹了springboot 啟動(dòng)如何排除某些bean的注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java使用Redisson分布式鎖實(shí)現(xiàn)原理
Redisson分布式鎖 之前的基于注解的鎖有一種鎖是基本redis的分布式鎖,這篇文章主要介紹了Java使用Redisson分布式鎖實(shí)現(xiàn)原理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
AQS加鎖機(jī)制Synchronized相似點(diǎn)詳解
這篇文章主要為大家介紹了AQS加鎖機(jī)制Synchronized相似點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

