Avatar ·

Qual é a diferença entre Thread e Coroutine em Kotlin?

Olá a todos! Comecei a estudar Kotlin e coroutines, mas não consigo entender a diferença fundamental entre uma Thread normal e uma Coroutine. Ambas parecem permitir executar código em paralelo ou de forma assíncrona. Li a documentação, mas está complicada. Por exemplo, escrevi um exemplo simples com threads: ```kotlin fun main() { println("Main starts: ${Thread.currentThread().name}") val thread = Thread { println("Thread working: ${Thread.currentThread().name}") Thread.sleep(1000) println("Thread done") } thread.start() println("Main continues: ${Thread.currentThread().name}") thread.join() println("Main ends") } ``` E o equivalente com coroutines: ```kotlin fun main() = runBlocking { println("Main starts: ${Thread.currentThread().name}") launch { println("Coroutine working: ${Thread.currentThread().name}") delay(1000) println("Coroutine done") }