您是否知道 Kotlin 中无法同步内联函数?我找不到任何相关文档。
假设您有一个带有同步方法的类;
/**
* 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()))
}
当此函数内联
时,此测试失败,当它未内联
时,则通过。
@Test
fun `safeSet - is synchronized`() {
val insideSet = AtomicInteger()
val threadsRun = CountDownLatch(2)
val t1 = Thread({
threadsRun.countDown()
sut.safeSet { currentValue: Int ->
insideSet.incrementAndGet()
try {
Thread.sleep(100000)
} catch (interrupted: InterruptedException) {
BoseLog.debug(interrupted)
}
currentValue + 1
}
})
t1.start()
val t2 = Thread({
threadsRun.countDown()
sut.safeSet { currentValue: Int ->
insideSet.incrementAndGet()
try {
Thread.sleep(100000)
} catch (interrupted: InterruptedException) {
BoseLog.debug(interrupted)
}
currentValue + 2
}
})
t2.start()
threadsRun.await()
Thread.sleep(100)
assertEquals(1, insideSet.get())
t1.interrupt()
t2.interrupt()
Thread.sleep(100)
assertEquals(2, insideSet.get())
}
最佳答案
@Synchronized
注释告诉编译器在方法上生成 ACC_SYNCHRONIZED
标志。内联函数不会编译为方法,因此注释确实被忽略。
有一个open issue在 Kotlin 中可以更好地处理这种情况。
关于Kotlin 同步内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53089859/