The hungry type

object Singleton
Copy the code

Thread-safe slob

class Singleton private constructor() {

    companion object {
        private var instance: Singleton? = null
            get() {
                if (field == null) field = Singleton()
                return field
            }

        @Synchronized
        fun instance(a): Singleton {
            returninstance!! }}}Copy the code

Double check lock type

class KtSingleton3 private constructor() {
    companion object {
        val instance by lazy { KtSingleton3() }
    }
}
Copy the code

Lazy is a function that takes a lambda and returns an instance of Lazy, which can be used as a delegate to implement the delay property. The first call to get() executes the lambda expression passed to lazy() and records the result; subsequent calls to get() simply return the recorded result. Lazy is the default thread mode LazyThreadSafetyMode. SYNCHRONIZED internal default double check the lock

Lazy internal implementation

public fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> =
     when (mode) {
       LazyThreadSafetyMode.SYNCHRONIZED -> SynchronizedLazyImpl(initializer)
       LazyThreadSafetyMode.PUBLICATION -> SafePublicationLazyImpl(initializer)
       LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer)
    }
Copy the code

Lazy interface

public interface Lazy<out T> {
    // The object is currently instantiated. Once instantiated, the object will not change
    public val value: T
    // Return true for delayed instantiation, false for not instantiation,
    // Once the method returns true, the method will always return true and will not be instantiated again
    public fun isInitialized(a): Boolean
}
Copy the code

SynchronizedLazyImpl

private class SynchronizedLazyImpl<out T>(initializer: () -> T, lock: Any? = null) : Lazy<T>, Serializable {
      private var initializer: (() -> T)? = initializer
      @Volatile private var _value: Any? = UNINITIALIZED_VALUE
      // final field is required to enable safe publication of constructed instance
      private vallock = lock ? :this
 
      override val value: T
          get() {
              val _v1 = _value
              // Check whether the function has been initialized. If it has been initialized, return it directly without calling the internal logic of the high-level function
              if(_v1 ! == UNINITIALIZED_VALUE) {@Suppress("UNCHECKED_CAST")
                  return _v1 as T
              }
 
              return synchronized(lock) {
                  val _v2 = _value
                  if(_v2 ! == UNINITIALIZED_VALUE) {@Suppress("UNCHECKED_CAST") (_v2 as T)
                 }
                  else {
                      // Call the advanced function to get its return value
                      valtypedValue = initializer!! (a)// Assign the return value to _value, which is used to return the value of the higher-level function
                      _value = typedValue
                     initializer = null
                      typedValue
                  }
              }
          }
          // Omit part of the code
  }
Copy the code

Static inner class

class Singleton private constructor() {
    companion object {
        val instance = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder = Singleton()
    }
}
Copy the code

Enumeration type

enum class Singleton {
    INSTANCE;
}
Copy the code