Skip to content

Instantly share code, notes, and snippets.

@ch8n
Last active July 17, 2021 09:27
Show Gist options
  • Save ch8n/7cb80ccb6fc512acce5ca6acaf25184b to your computer and use it in GitHub Desktop.
Save ch8n/7cb80ccb6fc512acce5ca6acaf25184b to your computer and use it in GitHub Desktop.
legacy Singleton threadsafe
class SingletonThreadSafe private constructor() {
companion object {
private var _instance: SingletonThreadSafe? = null
fun getInstance(): SingletonThreadSafe {
val cachedInstance = _instance
return cachedInstance ?: synchronized(this) {
cachedInstance ?: SingletonThreadSafe().also {
_instance = it
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment