Код: Выделить всё
import kotlin.concurrent.thread
class MultiThreads {
var counter = 0
fun main(){
val thread1 = thread {
(1..1000).forEach { _ ->
counter++
println("T1: $counter")
}
}
val thread2 = thread {
(1..1000).forEach { _ ->
counter++
println("T2: $counter")
}
}
thread1.join()
thread2.join()
println("counter=$counter")
}
}
...........
T1: 1998
T1: 1999
T1: 2000
counter=2000
---
AFAIK предполагается будет меньше 2000, не так ли?
Подробнее здесь: https://stackoverflow.com/questions/798 ... ad-context
Мобильная версия