Last active
July 17, 2021 09:27
-
-
Save ch8n/7cb80ccb6fc512acce5ca6acaf25184b to your computer and use it in GitHub Desktop.
legacy Singleton threadsafe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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