您的位置:首页 > 编程语言 > Java开发

【Java多线程】之八:单例模式的线程安全

2015-08-09 12:32 411 查看
Singleton is one of the most widely used creational design pattern to restrict the object creation by applications. In real world applications, resources like Database connections or Enterprise Information Systems (EIS) are limited and should be used wisely to avoid any resource crunch. To achieve this, we can implement Singleton design pattern to create a wrapper class around the resource and limit the number of object created at runtime to one.

ASingleton.java

package com.journaldev.designpatterns;

public class ASingleton{

private static ASingleton instance= null;
private static Object mutex= new Object();
private ASingleton(){
}

public static ASingleton getInstance(){
if(instance==null){
synchronized (mutex){
if(instance==null) instance= new ASingleton();
}
}
return instance;
}
}


这篇文章写得不错,点击查看!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: