java 线程单例_java单例模式,多线程下实现

单例模式

必备条件:

1:private的构造方法。

2:private static 对象保存该类实例。

3:static方法返回该类实例。

(一)饿汉模式

/**

* 单例模式

* 1:线程安全实现

* 2:浪费内存

* @author 祥少

*

*/

public class SingletonTest {

//final关键字,导致一旦被初始化,一直占用该段内存(即使你不使用)

public static final SingletonTest singletonTest = new SingletonTest();;

private SingletonTest() {

}

public static SingletonTest getSingleton() {

return singletonTest;

}

public static void main(String[] args) {

long current=System.currentTimeMillis();

for (int i = 0; i < 1000; i++) {

new Thread(new Runnable() {

@Override

public void run() {

System.out.println(SingletonTest.getSingleton().hashCode());

}

}).start();

}

if (Thread.activeCount() > 1) {

Thread.yield();

}

System.out.println("执行结束耗时:"+(System.currentTimeMillis()-current));

}

}

(二)饱汉模式

/**

* 单例模式

* 1:线程不安全

* getSingleton()函数线程不安全

*@author 祥少

*/

public class SingletonTest {

public static SingletonTest singletonTest = null;

private SingletonTest() {

}

public static SingletonTest getSingleton() {

if (singletonTest == null) {

singletonTest = new SingletonTest();

}

return singletonTest;

}

public static void main(String[] args) {

long current = System.currentTimeMillis();

for (int i = 0; i < 1000; i++) {

new Thread(new Runnable() {

@Override

public void run() {

System.out.println(SingletonTest.getSingleton().hashCode());

}

}).start();

}

// 判断线程是否结束

if (Thread.activeCount() > 1) {

Thread.yield();

}

System.out.println("执行结束耗时:" + (System.currentTimeMillis() - current));

}

}

线程安全改进方法:

(方法1)

public static synchronized SingletonTest getSingleton() {

if (singletonTest == null) {

singletonTest = new SingletonTest();

}

return singletonTest;

}

优点:synchronized关键字实现线程安全。

缺点:整个函数加锁效率略低(几乎等于单线程)。

(方法2:双加锁)

public static volatile SingletonTest singletonTest = null;

public static SingletonTest getSingleton() {

if (singletonTest == null) {

synchronized (SingletonTest.class) {

if (singletonTest == null) {

singletonTest = new SingletonTest();

}

}

}

return singletonTest;

}

最佳实现:效率高,线程安全,volatile保证操作原子性,只有%1的线程需要进入锁代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值