add singleton case
This commit is contained in:
parent
453c52987e
commit
6be24ea3b5
BIN
singleton_file
Normal file
BIN
singleton_file
Normal file
Binary file not shown.
52
src/main/java/io/github/ehlxr/singleton/SingleEnum.java
Normal file
52
src/main/java/io/github/ehlxr/singleton/SingleEnum.java
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2020 xrv <xrg@live.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package io.github.ehlxr.singleton;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 枚举单例模式
|
||||
*
|
||||
* @author ehlxr
|
||||
* @since 2021-02-18 10:28.
|
||||
*/
|
||||
public enum SingleEnum {
|
||||
/**
|
||||
* 唯一实例
|
||||
*/
|
||||
INSTANCE;
|
||||
|
||||
public static SingleEnum getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public String hello(String str) {
|
||||
return "hello " + str;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IntStream.range(0, 5).parallel().forEach(i -> System.out.println(Thread.currentThread().getName() + " => " + SingleEnum.getInstance()));
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2020 xrv <xrg@live.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package io.github.ehlxr.singleton;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* @author ehlxr
|
||||
* @since 2021-02-18 11:23.
|
||||
*/
|
||||
public class SingleTonStaticInnerClass {
|
||||
private SingleTonStaticInnerClass() {
|
||||
}
|
||||
|
||||
private static class HandlerInstance {
|
||||
private static SingleTonStaticInnerClass instance = new SingleTonStaticInnerClass();
|
||||
}
|
||||
|
||||
public static SingleTonStaticInnerClass getInstance() {
|
||||
return HandlerInstance.instance;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IntStream.range(0, 5).parallel().forEach(i -> System.out.println(Thread.currentThread().getName() + " => " + SingleTonStaticInnerClass.getInstance()));
|
||||
}
|
||||
|
||||
}
|
69
src/main/java/io/github/ehlxr/singleton/SingletonDcl.java
Normal file
69
src/main/java/io/github/ehlxr/singleton/SingletonDcl.java
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2020 xrv <xrg@live.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package io.github.ehlxr.singleton;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* double check lock
|
||||
* <p>
|
||||
* 懒汉模式
|
||||
*
|
||||
* @author ehlxr
|
||||
* @since 2021-02-18 10:23.
|
||||
*/
|
||||
public class SingletonDcl {
|
||||
private SingletonDcl() {
|
||||
}
|
||||
|
||||
private volatile static SingletonDcl INSTANCE;
|
||||
|
||||
public static SingletonDcl getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (SingletonDcl.class) {
|
||||
if (INSTANCE == null) {
|
||||
// 非原子性操作
|
||||
// 有可能会出现 JVM 优化指令重排序
|
||||
INSTANCE = new SingletonDcl();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
IntStream.range(0, 5).parallel().forEach(i -> System.out.println(Thread.currentThread().getName() + " => " + SingletonDcl.getInstance()));
|
||||
|
||||
// 反射破坏
|
||||
Constructor<SingletonDcl> dc = SingletonDcl.class.getDeclaredConstructor();
|
||||
dc.setAccessible(true);
|
||||
|
||||
System.out.println(dc.newInstance());
|
||||
System.out.println(dc.newInstance());
|
||||
System.out.println(dc.newInstance());
|
||||
}
|
||||
}
|
72
src/main/java/io/github/ehlxr/singleton/SingletonHungry.java
Normal file
72
src/main/java/io/github/ehlxr/singleton/SingletonHungry.java
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2020 xrv <xrg@live.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package io.github.ehlxr.singleton;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 饿汉模式
|
||||
*
|
||||
* @author ehlxr
|
||||
* @since 2021-02-18 10:20.
|
||||
*/
|
||||
public class SingletonHungry implements Serializable {
|
||||
private static final long serialVersionUID = -3654446367492818614L;
|
||||
|
||||
private SingletonHungry() {
|
||||
}
|
||||
|
||||
private static final SingletonHungry INSTANCE = new SingletonHungry();
|
||||
|
||||
public static SingletonHungry getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
// 防止 jdk 反序列化破坏单例
|
||||
private Object readResolve() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
IntStream.range(0, 5).parallel().forEach(i -> System.out.println(Thread.currentThread().getName() + " => " + SingleTonStaticInnerClass.getInstance()));
|
||||
|
||||
// 序列化破坏
|
||||
SingletonHungry instance = SingletonHungry.getInstance();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
|
||||
// 序列化【写】操作
|
||||
oos.writeObject(instance);
|
||||
File file = new File("singleton_file");
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
|
||||
|
||||
// 反序列化【读】操作
|
||||
SingletonHungry newInstance = (SingletonHungry) ois.readObject();
|
||||
System.out.println(instance);
|
||||
System.out.println(newInstance);
|
||||
System.out.println(instance == newInstance);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user