Is Kotlin `?.let` thread-safe?

有些话、适合烂在心里 提交于 2020-12-05 15:36:29

问题


Is Kotlin ?.let thread-safe?

Let's say a variable can be changed in different thread. Is using a?.let { /* */ } thread-safe? If it's equal to if (a != null) { block() } can it happen that in if it's not null and in block it's already null?


回答1:


a?.let { block() } is indeed equivalent to if (a != null) block().

This also means that if a is a mutable variable, then:

  1. a might be reassigned after the null check and hold a null value when block() is executed;

  2. All concurrency-related effects are in power, and proper synchronization is required if a is shared between threads to avoid a race condition;

However, as let { ... } actually passes its receiver as the single argument to the function it takes, it can be used to capture the value of a and use it inside the lambda instead of accessing the property again in the block(). For example:

a?.let { notNullA -> block(notNullA) }

// with implicit parameter `it`, this is equivalent to:
a?.let { block(it) }

Here, the value of a passed as the argument into the lambda is guaranteed to be the same value that was checked for null. However, observing a again in the block() might return a null or a different value, and observing the mutable state of the given instance should also be properly synchronized.



来源:https://stackoverflow.com/questions/57324180/is-kotlin-let-thread-safe

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