Home » Scala language

Multithreading in Scala

Scala Multithreading: Multithreading is using more than one threads at a time. Scala also supports multithreading to optimize the performance of the code. In this tutorial, we will learn the basics of multithreading in Scala.
Submitted by Shivang Yadav, on August 09, 2019

Scala Multithreading

Multithreading is the concept of using multiple threads simultaneously that allows the program to perform multiple operations simultaneously.

A thread is a lightweight sub-processes that require a very lesser amount of memory to get executed. The general multithreaded program consists of two or more thread that runs concurrently to optimize the use of resources ( multiple CPUs) and increase the performance of the program.

How thread works?

A thread is a lightweight process and its lifespan is defined as the time From which it starts to the point where it's terminated. In its lifespan, the thread goes from various phases. They are,

new → runnable → running → terminated

Sometimes the thread goes into the block state also.

Threads in Scala

To create a thread in Scala there are classes and methods defined. Threads in scala are created using two mechanisms,

  1. Extending the Thread class
  2. Extending the Runnable Interface

1) Creating a thread extending the Thread class

The thread class is an inbuilt Scala class that is extended to create threads. It has run() method, which is overrated to run the side object.

To create a thread we make an object of this class and then invoke the start() method which itself invokes the run() method.

Example:

class MyThread extends Thread 
{ 
    override def run(){ 
        println("Hello, This is Thread " + Thread.currentThread().getName() ) 
    } 
} 
    
object myObject { 
    def main(args: Array[String]){ 
        for (x <- 1 to 3){ 
            var th = new MyThread() 
            th.setName(x.toString()) 
            th.start() 
        } 
    } 
} 

Output

Hello, This is Thread 1
Hello, This is Thread 2
Hello, This is Thread 3

2) Creating a thread using runnable interface

Create thread using runnable interface to create a class that will implement the runnable interface and overwrites it's run() method to run a thread. Create a red Devil make an object of a class and then using this object we will call the start method that will initiate the thread and then automatically call the run() method that runs thread.

The below sample code shows how we create a thread using runnable interface:

class MyThread extends Runnable{ 
    override def run(){ 
        println("Hello, This is Thread " + Thread.currentThread().getName() )
    } 
} 

object myClass{ 
    def main(args: Array[String]){ 
        for (x <- 1 to 3){ 
            var newThread = new Thread(new MyThread()) 
            newThread.setName(x.toString()) 
            newThread.start() 
        } 
    } 
} 

Output

Hello, This is Thread 1
Hello, This is Thread 2
Hello, This is Thread 3





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.