Kotlin的单例线程安全吗? [英] Are Kotlin's singletons thread safe?

查看:590
本文介绍了Kotlin的单例线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kotlin单例(更具体地讲,对象声明)是否在构造上是线程安全的?如果没有,那么在Kotlin中编写线程安全单例的最佳实践是什么?

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.

推荐答案

科特林的对象"在构造上是线程安全的.您可以在任何反编译/转储工具中看到,声明的对象只是具有静态实例初始化和语言语法糖的最终类,从而简化了实例访问

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

这篇关于Kotlin的单例线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆