线程安全的单例模式
2012-07-05 23:24
246 查看
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public class Singleton
{
private static class SingletonHolder
{
static Singleton instance = new Singleton();
}
public static Singleton getInstance()
{
return SingletonHolder.instance;
}
}
我如何才能知道这段代码就是线程安全的呢?有谁能帮忙写个测试代码给我测试一下吗?最好是有屏幕输出语句的。
问题补充:
首先非常感谢各位的关注。但我已经弄明白了。把我的意见贴出来大家交流一下。
单例设计模式有两种形式:一种是饥饿式
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public final class EagerSingleton
{
private static EagerSingleton singObj = new EagerSingleton();
private EagerSingleton()
{
}
public static EagerSingleton getSingleInstance()
{
return singObj;
}
}
比较明显,这种是线程安全的。因为static变量是在类被加载时(有可能没被实例化)初始化并仅被初始化一次。因为这样就可以保证只有一个singObj被初始化。
另外一种则是懒汉式
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public final class LazySingleton
{
private static LazySingleton singObj = null;
private LazySingleton()
{
}
public static LazySingleton getSingleInstance()
{
if (singObj == null)
{
singObj = new LazySingleton();
}
return singObj;
}
这种则是线程不安全的了。因为当有多个线程一起调用getSingleInstance()时则可能会生成多个实例。因此才需要修改一下。修改为我之前的那段代码
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public class Singleton
{
private static class SingletonHolder
{
static Singleton instance = new Singleton();
}
public static Singleton getInstance()
{
return SingletonHolder.instance;
}
}
一个静态内部类内的一个静态成员就可以保证它只在类被加载时只初始化一次。因此不管有多少个线程来调用它,都只能得到同个实例(类被加载时初始化的那个)。
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public class Singleton
{
private static class SingletonHolder
{
static Singleton instance = new Singleton();
}
public static Singleton getInstance()
{
return SingletonHolder.instance;
}
}
package net.chopsuey.singleton; public class Singleton { private static class SingletonHolder { static Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } }
我如何才能知道这段代码就是线程安全的呢?有谁能帮忙写个测试代码给我测试一下吗?最好是有屏幕输出语句的。
问题补充:
首先非常感谢各位的关注。但我已经弄明白了。把我的意见贴出来大家交流一下。
单例设计模式有两种形式:一种是饥饿式
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public final class EagerSingleton
{
private static EagerSingleton singObj = new EagerSingleton();
private EagerSingleton()
{
}
public static EagerSingleton getSingleInstance()
{
return singObj;
}
}
package net.chopsuey.singleton; public final class EagerSingleton { private static EagerSingleton singObj = new EagerSingleton(); private EagerSingleton() { } public static EagerSingleton getSingleInstance() { return singObj; } }
比较明显,这种是线程安全的。因为static变量是在类被加载时(有可能没被实例化)初始化并仅被初始化一次。因为这样就可以保证只有一个singObj被初始化。
另外一种则是懒汉式
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public final class LazySingleton
{
private static LazySingleton singObj = null;
private LazySingleton()
{
}
public static LazySingleton getSingleInstance()
{
if (singObj == null)
{
singObj = new LazySingleton();
}
return singObj;
}
package net.chopsuey.singleton; public final class LazySingleton { private static LazySingleton singObj = null; private LazySingleton() { } public static LazySingleton getSingleInstance() { if (singObj == null) { singObj = new LazySingleton(); } return singObj; }
这种则是线程不安全的了。因为当有多个线程一起调用getSingleInstance()时则可能会生成多个实例。因此才需要修改一下。修改为我之前的那段代码
Java代码
![](http://www.iteye.com/images/icon_copy.gif)
![](http://www.iteye.com/images/icon_star.png)
![](http://www.iteye.com/images/spinner.gif)
package net.chopsuey.singleton;
public class Singleton
{
private static class SingletonHolder
{
static Singleton instance = new Singleton();
}
public static Singleton getInstance()
{
return SingletonHolder.instance;
}
}
package net.chopsuey.singleton; public class Singleton { private static class SingletonHolder { static Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } }
一个静态内部类内的一个静态成员就可以保证它只在类被加载时只初始化一次。因此不管有多少个线程来调用它,都只能得到同个实例(类被加载时初始化的那个)。
相关文章推荐
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式是否真的安全(2)
- 单例模式的几种写法:线程安全不安全
- 线程安全的单例模式
- Java线程安全中的单例模式
- 线程安全的单例模式
- 常见的创建线程安全单例模式的方法
- java线程安全的单例模式
- Java线程安全的单例模式的几种实现
- java 单例模式之线程安全的饿汉模式和懒汉模式
- 线程安全的单例模式
- Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。
- java 线程-生命周期-锁-阻塞-生成消费模式-安全容器
- java线程安全之多线程的设计模式(十四)
- 关于单例模式线程安全问题
- java多线程之线程安全的单例模式
- 浅议单例模式之线程安全
- 线程安全-单例模式
- Java之线程安全的单例模式