懒汉单例、线程不安全测试实例
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.singleton;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 懒汉式单例:延迟加载(线程不安全)
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-09-24 10:29
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class LazySingleton {
|
||||||
|
private static LazySingleton lazySingleton = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单例模式构造器必须是屏蔽的
|
||||||
|
*/
|
||||||
|
private LazySingleton() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程不安全
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static LazySingleton getInstance() {
|
||||||
|
if (lazySingleton == null) {
|
||||||
|
log.debug("LazySingleton实例化");
|
||||||
|
lazySingleton = new LazySingleton();
|
||||||
|
}
|
||||||
|
return lazySingleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程不安全验证
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static LazySingleton getInstance2() throws InterruptedException {
|
||||||
|
if (lazySingleton == null) {
|
||||||
|
// 使用线程等待模拟复杂实例化过程,让多线程同时进入该方法
|
||||||
|
Thread.sleep(1000);
|
||||||
|
log.debug("LazySingleton实例化");
|
||||||
|
lazySingleton = new LazySingleton();
|
||||||
|
}
|
||||||
|
return lazySingleton;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.singleton;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-09-24 10:53
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class MyRunnable implements Runnable {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
LazySingleton lazySingleton = null;
|
||||||
|
try {
|
||||||
|
lazySingleton = LazySingleton.getInstance2();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.creational.singleton;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
class LazySingletonTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 懒汉式注重延迟加载,当使用时才实例化
|
||||||
|
* 可以使用多线程测试方法或者通过多线程Debug方式测试
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void getInstanceOneThread() {
|
||||||
|
LazySingleton lazySingleton = LazySingleton.getInstance();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getInstanceMutilThread() throws InterruptedException {
|
||||||
|
|
||||||
|
Thread thread1 = new Thread(new MyRunnable());
|
||||||
|
Thread thread2 = new Thread(new MyRunnable());
|
||||||
|
|
||||||
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
|
||||||
|
Thread.sleep(3000);
|
||||||
|
log.debug("finish");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user