Here's a class Worker that extends Thread.
class Worker: Thread() {
override fun run() {
try {
sleep(200)
} catch (e: InterruptedException) {
println("It was interrupted in the sleeping state")
return
}
var i: Int = 0
if (isInterrupted) {
println("It was interrupted after the sleeping time")
return
}
while (i < 100_000) {
if (isInterrupted) {
println("It was interrupted inside a loop")
return
}
i++
}
println("Finished")
}
}
Another thread calls the interrupt() method of this thread.
fun main() {
val worker = Worker()
worker.start()
Thread.sleep(100)
worker.interrupt()
}
What does Worker print if another thread interrupts it?