Skip to content

Thread Functionalities : Creating, Starting and Stopping a thread

Devrath edited this page Feb 12, 2024 · 2 revisions

Starting a single thread

code

fun main(args: Array<String>) {

    println("<---------------Start of execution--------------->")

    val runnable = Runnable {
        println("Runnable is running in {${Thread.currentThread().name}}")
    }

    val thread = Thread(runnable)
    thread.name = "My-Thread-Name"
    thread.start()

    thread.join()

    println("<---------------End of execution----------------->")
    
}

Output

<---------------Start of execution--------------->
Runnable is running in {My-Thread-Name}
<---------------End of execution----------------->

Starting multiple threads

Observation

  • The order in which the runnable's execute is not guaranteed since we are not sure about the order in which the CPU executes the runnable`s

code

fun main(args: Array<String>) {

    println("<---------------Start of execution--------------->")

    val runnable1 = Runnable {
        println("Runnable1 is running in {${Thread.currentThread().name}}")
    }

    val runnable2 = Runnable {
        println("Runnable2 is running in {${Thread.currentThread().name}}")
    }

    val thread1 = Thread(runnable1)
    thread1.name = "My-Thread-1"
    val thread2 = Thread(runnable2)
    thread2.name = "My-Thread-2"


    thread1.start()
    thread2.start()

    thread1.join()
    thread2.join()

    println("<---------------End of execution----------------->")

}

Output

<---------------Start of execution--------------->
Runnable1 is running in {My-Thread-1}
Runnable2 is running in {My-Thread-2}
<---------------End of execution----------------->

Sleeping a thread

code

fun main(args: Array<String>) {

    println("<---------------Start of execution--------------->")

    val runnable1 = Runnable {
        println("Runnable1 has started running on {${Thread.currentThread().name}}")
        try{
            Thread.sleep(5000)
        }catch (ex:Exception){
            ex.printStackTrace()
        }
        println("Runnable1 has started running on {${Thread.currentThread().name}}")
    }

    val thread1 = Thread(runnable1)
    thread1.name = "My-Thread-1"
    thread1.start()

    thread1.join()

    println("<---------------End of execution----------------->")

}

Output

<---------------Start of execution--------------->
Runnable1 is running in {My-Thread-1}
Runnable2 is running in {My-Thread-2}
<---------------End of execution----------------->

Stopping a thread

code

fun main(args: Array<String>) {

    println("<---------------Start of execution--------------->")
    val myRunnableClass = MyRunnableClass()
    val myThread = Thread(myRunnableClass)
    myThread.start()

    try { Thread.sleep(5000) }catch (ex:Exception){ ex.printStackTrace() }

    println("<--Requesting to stop-->")
    myRunnableClass.requestStop()
    println("<--Thread is stopped-->")

    println("<---------------End of execution----------------->")

}


class MyRunnableClass : Runnable {

    private var isStopRequested = false

    fun requestStop() { synchronized(this){ isStopRequested = true } }

    private fun isStopRequested() : Boolean{ synchronized(this){ return isStopRequested } }

    private fun initiateSleep(long: Long){
        try { Thread.sleep(long) }catch (ex:Exception){ ex.printStackTrace() }
    }

    override fun run() {
        println("<---------------Runnable is running--------------->")
        while (!isStopRequested()){
            initiateSleep(1000)
            println("...")
        }
        println("<---------------Runnable has stopped--------------->")
    }

}

Output

<---------------Start of execution--------------->
<---------------Runnable is running--------------->
...
...
...
...
<--Requesting to stop-->
<--Thread is stopped-->
<---------------End of execution----------------->
...
<---------------Runnable has stopped--------------->