classFoot { privateval flag:AtomicInteger = AtomicInteger(0) @Throws(InterruptedException::class) funfirst(printFirst: Runnable) { // printFirst.run() outputs "first". Do not change or remove this line. printFirst.run() flag.incrementAndGet() }
@Throws(InterruptedException::class) funsecond(printSecond: Runnable) { while (flag.get() != 1) { } // printSecond.run() outputs "second". Do not change or remove this line. printSecond.run() flag.incrementAndGet() }
@Throws(InterruptedException::class) funthird(printThird: Runnable) { while (flag.get() != 2) { } // printThird.run() outputs "third". Do not change or remove this line. printThird.run() flag.incrementAndGet() }
} funmain(){ val foot = Foot() thread { val tow = Foot.PrintRunnable("tow") foot.second(tow) } thread { val three = Foot.PrintRunnable("three") foot.third(three) } thread { val one = Foot.PrintRunnable("one") foot.first(one) } }