单例模式JDK、Spring、mybatis源码样例
This commit is contained in:
@@ -2,7 +2,9 @@ package top.fjy8018.designpattern.pattern.creational.singleton;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -10,6 +12,11 @@ import java.util.Map;
|
||||
* 基于容器(集合)实现单例
|
||||
* 适用于类加载时有多个对象需要实例化场景
|
||||
*
|
||||
* JDK源码样例:{@link Desktop#getDesktop()}
|
||||
* 其{@link sun.awt.AppContext} 通过{@link HashMap}实现,使用同步保证单例线程安全
|
||||
* Spring 源码样例:{@link AbstractFactoryBean#getObject()} 在156行判断当前类是否已经实例化
|
||||
* 若没有实例化则在{@link AbstractFactoryBean#getEarlySingletonInstance()}中通过代理方式实例化对象
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-24 22:31
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,8 @@ import java.io.Serializable;
|
||||
* 优点:写法简单,类加载时初始化,线程安全
|
||||
* 缺点:若类不被使用则会造成内存浪费
|
||||
*
|
||||
* JDK源码样例:{@link Runtime} 其实例在类加载时实例化并通过{@link Runtime#getRuntime()} 获取
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-24 15:38
|
||||
*/
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package top.fjy8018.designpattern.pattern.creational.singleton;
|
||||
|
||||
import org.apache.ibatis.executor.ErrorContext;
|
||||
|
||||
/**
|
||||
* 基于{@link ThreadLocalInstance} 的(伪)单例模式,只能保证线程内单例唯一(空间换时间思想)
|
||||
*
|
||||
* mybatis源码样例:{@link ErrorContext#instance()} 返回通过ThreadLocal实现的单例对象LOCAL
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-25 16:24
|
||||
*/
|
||||
|
||||
@@ -57,7 +57,7 @@ class ContainerSingletonTest {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Object singleton = ContainerSingleton.getInstance("object");
|
||||
log.info(Thread.currentThread().getName() + " " + singleton);
|
||||
log.info(" " + singleton);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ class ContainerSingletonTest {
|
||||
}
|
||||
|
||||
Object singleton = ContainerSingletonHashtable.getInstance("object");
|
||||
log.info(Thread.currentThread().getName() + " " + singleton);
|
||||
log.info(" " + singleton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class LazyDoubleCheckSingletonTest {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
||||
log.info(" " + lazySingleton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class LazySingletonSynchronizedTest {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
||||
log.info(" " + lazySingleton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class LazySingletonTest {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
||||
log.info(" " + lazySingleton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class StaticInnerClassSingletonTest {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
||||
log.info(" " + lazySingleton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ class ThreadLocalInstanceTest {
|
||||
// 在主线程中取
|
||||
for (int i = 0; i < 5; i++) {
|
||||
ThreadLocalInstance instance = ThreadLocalInstance.getInstance();
|
||||
log.info(Thread.currentThread().getName() + " " + instance);
|
||||
log.info(" " + instance);
|
||||
}
|
||||
|
||||
Thread thread1 = new Thread(new ThreadLocalInstanceTest.MyRunnable());
|
||||
@@ -40,7 +40,7 @@ class ThreadLocalInstanceTest {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info(Thread.currentThread().getName() + " " + instance);
|
||||
log.info(" " + instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user