Scala | Multithreading Last Updated : 08 Apr, 2019 Comments Improve Suggest changes Like Article Like Report A process in which multiple threads executing simultaneously that is called multithreading. It allows you to perform multiple tasks independently. What are Threads in Scala? Threads are lightweight sub-processes which occupy less memory. A multi-threaded program contains two or more threads that can run concurrently and each thread can handle a different task at the same time making optimal use of the available resources specially when your system(computer) has multiple CPUs. Multithreading is used to develop concurrent applications in Scala. Threads in Scala can be created by using two mechanisms : Extending the Thread class Extending the Runnable Interface Thread creation by extending the Thread class We create a class that extends the Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object. Scala // Scala code for thread creation by extending // the Thread class class MyThread extends Thread { override def run() { // Displaying the thread that is running println("Thread " + Thread.currentThread().getName() + " is running.") } } // Creating object object GFG { // Main method def main(args: Array[String]) { for (x <- 1 to 5) { var th = new MyThread() th.setName(x.toString()) th.start() } } } Output : Thread 1 is running. Thread 2 is running. Thread 3 is running. Thread 4 is running. Thread 5 is running. Thread creation by Extending Runnable Interface We create a new class which extends Runnable interface and override run() method. Then we instantiate a Thread object passing the created class to the constructor. We then call start() method on this object. Scala // Scala code for thread creation by implementing // the Runnable Interface class MyThread extends Runnable { override def run() { // Displaying the thread that is running println("Thread " + Thread.currentThread().getName() + " is running.") } } // Creating object object MainObject { // Main method def main(args: Array[String]) { for (x <- 1 to 5) { var th = new Thread(new MyThread()) th.setName(x.toString()) th.start() } } } Output : Thread 1 is running. Thread 3 is running. Thread 4 is running. Thread 2 is running. Thread 5 is running. Note : threads need not be running in any sequential order. All the threads run concurrently and independent of each other. Scala Thread Life Cycle In between the period of creation and Termination of a Scala Thread, the thread undergoes various state changes. These constitute the Life Cycle of a Scala Thread. It has the five following states. New : This is the first state when the Thread is just created. Runnable : This is the state when the Thread has been created but the Thread has not got the chance to start running. Running : In this state the Thread is performing its task. Blocked (or Waiting): This is the state when the thread is still alive, but is currently unable to run due to waiting for input or resources. Terminated : A thread is in dead state when its run() method exits. Comment More info K Kaustav kumar Chanda Follow Improve Article Tags : Scala Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like