饿汉式单例

java实现

  1. public class PlainOldSingleton {
  2. private static PlainOldSingleton INSTANCE = new PlainOldSingleton();
  3. private PlainOldSingleton(){
  4. System.out.println("PlainOldSingleton");
  5. }
  6. public static PlainOldSingleton getInstance(){
  7. return INSTANCE;
  8. }
  9. }

kotlin实现

  1. object PlainOldSingleton {
  2. }

非线程安全的懒汉式单例

java实现

  1. public class LazyNotThreadSafe {
  2. private static LazyNotThreadSafe INSTANCE;
  3. private LazyNotThreadSafe(){}
  4. public static LazyNotThreadSafe getInstance(){
  5. if(INSTANCE == null){
  6. INSTANCE = new LazyNotThreadSafe();
  7. }
  8. return INSTANCE;
  9. }
  10. }

kotlin实现

  1. class LazyNotThreadSafe {
  2. companion object{
  3. val instance by lazy(LazyThreadSafetyMode.NONE) {
  4. LazyNotThreadSafe()
  5. }
  6. //下面是另一种等价的写法, 获取单例使用 get 方法
  7. private var instance2: LazyNotThreadSafe? = null
  8. fun get() : LazyNotThreadSafe {
  9. if(instance2 == null){
  10. instance2 = LazyNotThreadSafe()
  11. }
  12. return instance2!!
  13. }
  14. }
  15. }

线程安全的懒汉式单例

java实现

  1. public class LazyThreadSafeSynchronized {
  2. private static LazyThreadSafeSynchronized INSTANCE;
  3. private LazyThreadSafeSynchronized(){}
  4. public static synchronized LazyThreadSafeSynchronized getInstance(){
  5. if(INSTANCE == null){
  6. INSTANCE = new LazyThreadSafeSynchronized();
  7. }
  8. return INSTANCE;
  9. }
  10. }

kotlin实现

  1. class LazyThreadSafeSynchronized private constructor() {
  2. companion object {
  3. private var instance: LazyThreadSafeSynchronized? = null
  4. @Synchronized
  5. fun get(): LazyThreadSafeSynchronized{
  6. if(instance == null) instance = LazyThreadSafeSynchronized()
  7. return instance!!
  8. }
  9. }
  10. }

DoubleCheck

java实现

  1. public class LazyThreadSafeDoubleCheck {
  2. private static volatile LazyThreadSafeDoubleCheck INSTANCE;
  3. private LazyThreadSafeDoubleCheck(){}
  4. public static LazyThreadSafeDoubleCheck getInstance(){
  5. if(INSTANCE == null){
  6. synchronized (LazyThreadSafeDoubleCheck.class){
  7. if(INSTANCE == null) {
  8. //初始化时分为实例化和赋值两步, 尽管我们把这一步写成下面的语句,
  9. // 但Java虚拟机并不保证其他线程『眼中』这两步的顺序究竟是怎么样的
  10. INSTANCE = new LazyThreadSafeDoubleCheck();
  11. }
  12. }
  13. }
  14. return INSTANCE;
  15. }
  16. }

kotlin实现

  1. class LazyThreadSafeDoubleCheck private constructor(){
  2. companion object{
  3. val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
  4. LazyThreadSafeDoubleCheck()
  5. }
  6. private @Volatile var instance2: LazyThreadSafeDoubleCheck? = null
  7. fun get(): LazyThreadSafeDoubleCheck {
  8. if(instance2 == null){
  9. synchronized(this){
  10. if(instance2 == null)
  11. instance2 = LazyThreadSafeDoubleCheck()
  12. }
  13. }
  14. return instance2!!
  15. }
  16. }
  17. }

静态内部类单例

java实现

  1. public class LazyThreadSafeStaticInnerClass {
  2. private static class Holder{
  3. private static LazyThreadSafeStaticInnerClass INSTANCE = new LazyThreadSafeStaticInnerClass();
  4. }
  5. private LazyThreadSafeStaticInnerClass(){}
  6. public static LazyThreadSafeStaticInnerClass getInstance(){
  7. return Holder.INSTANCE;
  8. }
  9. }

kotlin实现

  1. class LazyThreadSafeStaticInnerObject private constructor(){
  2. companion object{
  3. fun getInstance() = Holder.instance
  4. }
  5. private object Holder{
  6. val instance = LazyThreadSafeStaticInnerObject()
  7. }
  8. }