Kotlin同步Inine

时间:2018-10-31 18:26:05

标签: kotlin

是否知道您无法在Kotlin中同步内联函数?我找不到与此有关的任何文档。

假设您有一个带有同步方法的类;

lst1

当此功能为lst2时,此测试失败;如果不是/** * Allows modifying the value in a synchronized way so that the get() and set() are atomic. * * Note: anything synchronized cannot be `inline`. */ @Synchronized fun safeSet(calculateNewValue: (T) -> T) { set(calculateNewValue(get())) } ,则该测试通过。

inlined

1 个答案:

答案 0 :(得分:5)

@Synchronized注释告诉编译器在方法上生成ACC_SYNCHRONIZED标志。内联函数不会编译为方法,因此注释确实会被忽略。

Kotlin中有一个open issue,可以更好地处理这种情况。