Creating and Synchronizing Two Threads in Kotlin

Report a typo

Our code generates and starts two concurrent threads, labelled as thread1 and thread2. Each thread, after being initiated, displays a message confirming that it is operational, then takes a pause for 1000 milliseconds (1 second), and following the pause, shows a message informing that it has finished. Finally, the main thread patiently waits for the termination of both threads using the join() method for each one. Once both threads have finished, the main thread shows a message affirming that both threads have executed and finished their tasks.

Order the code lines correctly to run the program.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                val thread1 = thread(start = true, name = "Thread-1") {
              
                println("Thread-2 finished")
              
                println("Both threads have finished execution")
              
                }
              
                Thread.sleep(1000)
              
                Thread.sleep(1000)
              
                import kotlin.concurrent.thread
              
                println("Thread-1 finished")
              
                }
              
                println("Thread-2 is running")
              
                fun main() {
              
                println("Thread-1 is running")
              
                }
              
                thread2.join()
              
                val thread2 = thread(start = true, name = "Thread-2") {
              
                thread1.join()
              

Create a free account to access the full topic