Some amazing Thread Methods which will make your task easy

Bearded confident maintenance engineer in white shirt is working in database center
Reading Time: 3 minutes

Multithreading is one of the very important concepts in Scala programming. It allows us to create multiple threads in our program to perform various tasks. Now Scala Thread class provides us many methods which make our tasks easy. In this blog, we are going to discuss a few of these methods.

Threads - Thread Methods Scala

Scala Thread Methods

Scala threads provide us various methods to deal with threads. In this blog, we are going to discuss a few of them.

start() Thread Method

This method basically starts the execution of a thread. Basically, it changes the state of the threads from New State to Runnable state. After this whenever the thread gets a chance to execute it will call the run method. Let’s take this code as an example

class Method1 extends Thread {
  override def run(): Unit ={
    println("Hello, This is Thread by extending Thread class ")
  }
}
object main extends App {
    val th1 = new Method1()
    th1.start()     //  .start() method
}
Output:
Hello, This is Thread by extending Thread class

run() Method

This method performs tasks on the threads. Let’s take this code as an example

class Method1 extends Thread {
  override def run(): Unit ={    // run() method
    println("Hello, This is Thread by extending Thread class ")
  }
}

getName() and setName(String) Methods

setName(String) method gives a name to threads whereas the getName() method returns the name of the threads. Let’s take this code as an example

class Name extends Thread{
  override def run(): Unit ={
      println("Thread name is " +this.getName)   // getName
  }
}

object Name extends App {
  var thread1= new Name()
  thread1.setName("Thread-101")      // setName()
  thread1.start()
}
OUTPUT:
Thread name is Thread-101

currentThread() Method

The currentThread() is used to return the reference of the currently executing thread. Let’s take this code as an example

class CurrentThread extends Thread{
  override def run(): Unit ={
    Thread.currentThread().setName("Current-Thread")
    println("Thread name is " + Thread.currentThread().getName)
    }
}

object CurrentThread extends App{
  var thread= new CurrentThread()
  thread.start()
}
Output:
Thread name is Current-Thread

getPriority() and setPriority(int priority) Methods

By using these methods, we can get and set the priority of threads. Let’s take this code as an example

class PriorityMethod extends Thread {

  override def run(): Unit = {
    println("Priority of this thread is "+ this.getPriority) // getPriority
  }
}
object PriorityMethod extends App {
  var thread1= new Thread(new PriorityMethod())
  thread1.setPriority(5)   // setPriority()
  thread1.start()
}
Output:
Priority of this thread is 5

sleep() Method

This method is used to put threads to sleep for a number of milliseconds. In the sleep() method we take time in milliseconds as an argument. Let’s take this code as an example

class Sleep extends Thread {
  override def run(): Unit = {
    for (i <- 1 to 5) {
      println("After " + i + " sec")
      Thread.sleep(1000)  // sleep()
    }
  }
}
object Sleep extends App {
  var thread1= new Sleep()
  thread1.start()
}
Output:
After 1 sec
After 2 sec
After 3 sec
After 4 sec
After 5 sec

join() Method

join() method is used to hold the execution of the thread which is currently running until it finishes the execution. It also waits for a thread to die before it can start more. Let’s take this code as an example

class Join extends Thread{
  override def run(): Unit = {
    for (i <- 1 to 2) {
      println(i)
      Thread.sleep(1000)
    }
  }
}

object Join extends App{
  var thread1 = new Join()
  var thread2 = new Join()
  thread1.start()
  thread1.join()    // join() method
  thread2.start()
}
Output:
1
2
1
2

isAlive() Method

isAlive() method checks that weather a thread is alive or not. A thread is said to be alive when it has been started but has not reached a dead state. Let’s take this code as an example

class Alive extends Thread{
  override def run(): Unit = {
      println("Is "+ Thread.currentThread().getName + " alive -> " + Thread.currentThread().isAlive)   // isAlive method
  }
}
object main extends App {
  var thread1 = new Thread(new Alive())
  thread1.setName("Thread1")
  thread1.start()
}
Output:
Is Thread1 alive -> true

getId Method

This method returns the id of the threads. Let’s take this code as an example

class ID extends Thread{
  override def run(): Unit = {
      println("Id of this thread is "+Thread.currentThread().getId) 
// getId method
  }
}
object ID extends App {
  var thread1 = new Thread(new ID())
  thread1.start()
}

Conclusion

In this blog, we get to know about some scala thread methods and how to use them. If you want to know more about multithreading visit these sites

https://www.geeksforgeeks.org/scala-multithreading/

Written by 

Divyansh Devrani is a Software Consultant having experience in the Scala Ecosystem. He has worked on technologies like Scala, Akka, Play, and Postgre. He is recognized as a good team player who wants to interact with new technologies. He loves to learn new things His hobbies include listening to muisc, watching movies and anime.