Sobes.tech
Junior — Senior

Possible output results from multiple threads

livecode

Task condition

Determine what text options may appear in the console after running the program, considering the operation of two threads that modify shared object fields.

class State {
    var x = 0
    var y = 0
}

fun main() {
    val state = State()

    val t1 = Thread {
        state.x = 1
        println(state.y)
    }

    val t2 = Thread {
        state.y = 1
        println(state.x)
    }

    t1.start()
    t2.start()
    t1.join()
    t2.join()
}