码迷,mamicode.com
首页 > 编程语言 > 详细

Java_单例模式

时间:2014-11-19 18:31:39      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   color   使用   sp   java   for   strong   

主要介绍单例模式的一种写法、注意事项、作用、测试,以Java语言为例,下面代码是目前见过最好的写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Singleton {
 
    private static volatile Singleton instance = null;
 
    // private constructor suppresses
    private Singleton(){
    }
 
    public static Singleton getInstance() {
        // if already inited, no need to get lock everytime
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
 
        return instance;
    }
}

1、需要注意的点

其中需要注意的点主要有三点
(1) 私有化构造函数
(2) 定义静态的Singleton instance对象和getInstance()方法
(3) getInstance()方法中需要使用同步锁synchronized (Singleton.class)防止多线程同时进入造成instance被多次实例化
可以看到上面在synchronized (Singleton.class)外又添加了一层if,这是为了在instance已经实例化后下次进入不必执行synchronized (Singleton.class)获取对象锁,从而提高性能。

Ps: 也有实现使用的是private static Object    obj      = new Object();加上synchronized(obj),实际没有必要多创建一个对象。synchronized(X.class) is used to make sure that there is exactly one Thread in the block.

 

2、单例的作用
单例主要有两个作用
(1) 保持程序运行过程中该类始终只存在一个示例
(2) 对于new性能消耗较大的类,只实例化一次可以提高性能

 

3、单例模式测试

单例模式可以使用多线程并发进行测试,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) {
    final CountDownLatch latch = new CountDownLatch(1);
    int threadCount = 1000;
 
    for (int i = 0; i < threadCount; i++) {
        new Thread() {
 
            @Override
            public void run() {
                try {
                    // all thread to wait
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
 
                // test get instance
                System.out.println(Singleton.getInstance().hashCode());
            }
        }.start();
    }
 
    // release lock, let all thread excute Singleton.getInstance() at the same time
    latch.countDown();
}

其中CountDownLatch latch为闭锁,所有线程中都用latch.await();等待锁释放,待所有线程初始化完成使用latch.countDown();释放锁,从而达到线程并发执行Singleton.getInstance()的效果。

bubuko.com,布布扣

Java_单例模式

标签:http   io   ar   color   使用   sp   java   for   strong   

原文地址:http://www.cnblogs.com/changkai244/p/4108759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!