1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import kotlin.concurrent.thread fun main() { val myThread = thread { try { while (!Thread.currentThread().isInterrupted) { println("Hilo en ejecución") Thread.sleep(1000) } } catch (e: InterruptedException) { println("Hilo interrumpido") } } // Esperar 5 segundos y luego interrumpir el hilo Thread.sleep(5000) myThread.interrupt() // Esperar a que el hilo termine su ejecución myThread.join() println("Programa finalizado") } |