If 十只橘猫九只胖

Then 还有一只特别胖


  • 归档

  • 分类

  • 标签

  • 关于

单例模式的几种套路

发表于 2017-03-01 | | 阅读次数

套路1 - 立即加载 instantly initialization

1
2
3
4
5
6
7
8
9
10
11
public class EagerSingleton {
**private** **static** EagerSingleton instance = new EagerSingleton();
**private** EagerSingleton() {
}
public static EagerSingleton getInstance() {
return instance;
}
}

套路2 - 延迟初始化 lazy initialization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {
}
public static **synchronized** LazySingleton getInstance() {
if (instance == null) {
try {
Thread.sleep(1000 * 3);// mock long time init
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new LazySingleton();
}
return instance;
}
}

套路3 - 静态内部类 static Holder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class InnerHolderClass {
private InnerHolderClass() {
}
private static class InnerHolder {
static InnerHolderClass innerHolder = new InnerHolderClass();
}
public static InnerHolderClass getInstance() {
InnerHolderClass innerHolder = InnerHolder.innerHolder;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return innerHolder;
}
}

套路4 - 枚举 enum

1
2
3
public enum EnumSingleton {
SINGLE;
}

套路5 - 双重锁 double check

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class DoubleCheckSingleton {
//volatile is important!!!
//volatile is important!!!
//volatile is important!!!
private static **volatile** DoubleCheckSingleton instance;//volatile is important!!!
private DoubleCheckSingleton() {
}
public static DoubleCheckSingleton getInstance() {
if (null == instance) {
synchronized (DoubleCheckSingleton.class) {
if (null == instance) {
instance = new DoubleCheckSingleton();
}
}
}
return instance;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class EnhancedDoubleCheckSingleton {
private static EnhancedDoubleCheckSingleton instance;
private EnhancedDoubleCheckSingleton() {
}
//it's not the recommend way.we recommend lazy initialization inner class because the static instance field.
public static EnhancedDoubleCheckSingleton getInstance() {
**EnhancedDoubleCheckSingleton tmp = instance;**
if (null == tmp) {
synchronized (EnhancedDoubleCheckSingleton.class) {
**tmp = instance;**
if (null == tmp) {
instance = tmp = new EnhancedDoubleCheckSingleton();
}
}
}
return tmp;
}
}

本博客的入坑纪实-Hexo博客平台

发表于 2017-03-01 | | 阅读次数

java 线程的运行时异常处理-UncaughtExceptionHandler

发表于 2017-03-01 | 分类于 Java | | 阅读次数

Java 线程(Runnable)的异常处理, 以下的阐述均基于Runnable的线程实现

首先,有个问题,如果Java子线程发生异常会怎么样?实际上会导致该线程直接终止。当年自己写了个线程模型大概是这样的:

阅读全文 »

HashMap 源码分析

发表于 2017-03-01 | | 阅读次数

CocurrentHashMap 源码试解读

发表于 2017-03-01 | | 阅读次数

AbstractQueuedSynchronizer (AQS) 源码试解读

发表于 2017-03-01 | | 阅读次数

用Fork And Join框架计算Fibonacci数列

发表于 2017-03-01 | | 阅读次数

Fork And Join 的并发框架对与大型任务的分解计算是十分方便的。主要是将任务分割(fork)最后再合并(join)得到结果。
实际上使用了分治的思想。其中涉及到任务窃取算法等。

以下是一个计算Fibonacci数列的Demo,使用BigInteger是为了避免溢出,但是计算效率大大降低,可以用Long类型代替。

阅读全文 »

Java Object的 wait & notify & notifyAll 方法探索

发表于 2017-03-01 | | 阅读次数

wait 和 notify/notifyAll 主要是用于线程间通信的方法,即信号量的通信方法。他们是Java Object 的实例方法,所以可以用于各种引用类型。由于wait和notify/notifyAll必须在获取到monitor(锁)的区域内使用,所以,我们更多的是使用synchronized锁住线程共享的变量,并且在共享变量上做线程间通信。

阅读全文 »
12
Rick Ho

Rick Ho

RickyDuoli

18 日志
4 分类
26 标签
RSS
github 并发网 weibo
基友链接
  • 梅西
  • 蒙古包
© 2013 - 2017 Rick Ho
由 Hexo 强力驱动
主题 - NexT.Muse