Are Kotlin's singletons thread safe?

六眼飞鱼酱① 提交于 2019-12-22 01:26:06

问题


Are Kotlin singletons (more specifically, object declarations) thread-safe by construction? If not, what is the best practice to write thread safe singletons in Kotlin?

I would guess they are, but I haven't been able to find any explicit statement about it in the docs.


回答1:


Kotlin "object" is thread-safe by construction. As you can see in any decompile/dumping tool, declared object is just final class with static instance initialization + language syntax sugar to simplify instance access



来源:https://stackoverflow.com/questions/30179793/are-kotlins-singletons-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!