package com.zgh.gof23.singleton;
/**
* 饿汉式
* @author yuelin
*
*/
public class SingleDemo {
private static SingleDemo instance = new SingleDemo();
//私有化构造器
private SingleDemo() {
//防止其他通过反射调用构造方法,破解单例
if (instance != null) {
throw new RuntimeException();
}
}
//对外提供统一的访问点
public static SingleDemo getInstance() {
return instance;
}
}
package com.zgh.gof23.singleton;
/**
* 懒汉式实现单例
*
* @author zhuguohui
*
*/
public class SingleDemo2 {
// 此处并不初始化实例
private static SingleDemo2 instance;
private SingleDemo2() {
if (instance != null) {
throw new RuntimeException();
}
}
/**
* 当调用此方法的时候才初始化实例, 为了实现线程安全,需要使用同步方法
*
* @return
*/
public static synchronized SingleDemo2 getInstance() {
if (instance == null) {
instance = new SingleDemo2();
}
return instance;
}
}
package com.zgh.gof23.singleton;
/**
* 双重检查
*
* @author zhuguohui
*
*/
public class SingleDemo3 {
private static SingleDemo3 instance;
private SingleDemo3() {
if (instance != null) {
throw new RuntimeException();
}
}
public static SingleDemo3 getInstance() {
//第一重检查,提高效率
if (instance == null) {
synchronized (SingleDemo3.class) {
//第二重检查保证线程安全
if (instance == null) {
instance = new SingleDemo3();
}
}
}
return instance;
}
}
/**
* 静态内部类实现单例
*
* @author zhuguohui
*
*/
public class SingleDemo4 {
private static SingleDemo4 instance;
private static class SingleDemo4Holder {
private static final SingleDemo4 instance = new SingleDemo4();
}
private SingleDemo4() {
if (instance != null) {
throw new RuntimeException();
}
}
/**
* 调用这个方法的时候,JVM才加载静态内部类,才初始化静态内部类的类变量。由于由JVM初始化,保证了线程安全性,
* 同时又实现了懒加载
* @return
*/
public static SingleDemo4 getInstance() {
return SingleDemo4Holder.instance;
}
}
/**
* 枚举实现单例
* 枚举由JVM实现其的单例性
* @author zhuguohui
*
*/
public enum SingleDemo5 {
INSTANCE;
}
public class APP {
public static void main(String[] args) {
int threadCount = 100;
long start = System.currentTimeMillis();
final CountLock lock = new CountLock(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10000000; j++) {
//通过更换此处,来测试不同单例实现方式在多线程环境下的性能
SingleDemo5 demo = SingleDemo5.INSTANCE;
}
lock.finish();
}
}).start();
}
//等待所有线程执行完
lock.waitForWrok();
long end = System.currentTimeMillis();
System.out.println("总共耗时" + (end - start));
}
}
package com.zgh.gof23.singleton;
public class CountLock {
//线程的总数量
private int count;
public CountLock(int count) {
this.count = count;
}
/**
* 当一个线程完成任务以后,调用一次这个方法
*/
public synchronized void finish() {
count--;
if (count == 0) {
notifyAll();
}
}
/**
* 需要等待其他线程执行完的线程,调用此方法。
*/
public synchronized void waitForWrok() {
while (count > 0) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
| Tables | 实现方式 | 用时(毫秒) |
|---|---|---|
| 1 | 饿汉式 | 13 |
| 2 | 懒汉式 | 10778 |
| 3 | 双重检查 | 15 |
| 4 | 静态内部类 | 14 |
| 5 | 枚举 | 12 |
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有