Skip to content

Instantly share code, notes, and snippets.

@skywall
Last active October 25, 2020 12:30
Show Gist options
  • Save skywall/508c1bac9a015de3b6c3aeed77709207 to your computer and use it in GitHub Desktop.
Save skywall/508c1bac9a015de3b6c3aeed77709207 to your computer and use it in GitHub Desktop.
SingletonHolder
open class SingletonHolder<out T, in A>(private val constructor: (A) -> T) {
@Volatile private var instance: T? = null
fun getInstance(arg: A): T {
return when {
instance != null -> instance!!
else -> synchronized(this) {
if (instance == null) {
instance = constructor(arg)
}
instance!!
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment