Home > Back-end >  How do I know, the thread is finished in Kotlin?
How do I know, the thread is finished in Kotlin?

Time:05-25

Im trying to set text after all threads are finished. Do you have any ideas for me?

fun threadStart(){
var ende = 150_000
val s = Semaphore(1)

var thread1 :Thread = Thread(Runnable{
    while(counter < ende){
        counter  
        println("1. Thread : $counter")
        println("${Thread.currentThread().name} 1. Thread  $counter")
    }
    if(counter == ende) {
        while (counter != 0) {
            counter--

            println("${Thread.currentThread().name} der Minus counter $counter")
            s.release()
        }
    }
thread1.start()
thread2.start()
thread3.start()
thread4.start()

Normally I would write a print after thread4 here or in the main after threadstart()

CodePudding user response:

Are you asking how to make the main thread wait for the other threads to finish? If so, you can do that with:

thread1.join()
thread2.join()
thread3.join()
thread4.join()
  • Related