What is Scala Thread & Multithreading | File Handling in Scala

Scala course with real-time projects Start Now!!

In our last Scala tutorial, we discussed Scala Pattern Matching. Here, we divide this article into two sections- File Handling in Scala Programming Language and Scala Thread. Moreover, how can we write & read files in Scala and multithreading in Scala? Along with this, we will study what is Scala thread and how can we create a thread in Scala. At last, we learn Scala thread methods.

So, let’s start Scala Thread.

Scala File Handing

Scala lets us read from and write to files. Here, we will learn how to open files, read from them, and write to them. Let’s take a quick look.

How to Write Files in Scala?

Since Scala doesn’t have a class that will help us with writing files, we use java.io._ from Java.

C:\Users\lifei>cd Desktop

C:\Users\lifei\Desktop>scala

Refer For More Detail Scala File i/o: Open, Read and Write a File in Scala

Welcome to Scala 2.12.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_161).

Type in expressions for evaluation. Or try:help.

scala> import java.io._
import java.io._
scala> val writer=new PrintWriter(new File("demo.txt"))
writer: java.io.PrintWriter = java.io.PrintWriter@3c3e363
scala> writer.write("Successfully wrote to the file we created")
scala> writer.close()

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

This gives us the following file on the desktop:

Scala Multithreading | File Handling in Scala

Scala Multithreading | File Handling in Scala

How to Read Files in Scala?

To read from a file, we use scala.io.Source. Take a look.

scala> import scala.io.Source
import scala.io.Source
scala> Source.fromFile("demo.txt").mkString
res2: String = Successfully wrote to the file we created

But if the file had more than one line:

scala> Source.fromFile("demo.txt").getLines.foreach{x=>println(x)}

Successfully wrote to the file we created
Line 1
Line 2
Using an iterator:

scala> val it=Source.fromFile("demo.txt").getLines()
it: Iterator[String] = non-empty iterator
scala> it.next()
res4: String = Successfully wrote to the file we created
scala> it.next()
res5: String = Line 1
scala> it.next()
res6: String = Line 2

For more on file handling, read up on Scala File IO.
Let’s Learn Scala Map with Examples Quickly & Effectively

What is Scala Multithreading?

Scala supports multithreading, which means we can execute multiple threads at once. We can perform multiple operations independently and achieve multitasking. This lets us develop concurrent applications.

What is Scala Threads?

A thread is a lightweight sub-process occupying less memory. To create it, we can either extend the Thread class or the Runnable interface. We can use the run() method then. A thread holds less memory than a full-blown process. Let’s take a look at its life-cycle.

Read about Scala Closures with Examples

a. Scala Thread Life-Cycle

A Scala thread life-cycle is an account of when it starts to when it terminates. It has the following five states:

1. New- Scala thread is in this state when it is created. This is effectively the first state.

2. Runnable- When the thread has been created but the thread scheduler hasn’t selected it for running, it is in the runnable state.

3. Running- When the thread scheduler has selected a thread, it is running.

4. Blocked- In the blocked state, the thread is alive, but waiting for input or resources.

5. Terminated- When the run() method exits, the Scala thread is dead.

Scala Multithreading | File Handling in Scala

Scala Multithreading | File Handling in Scala

How to Create Thread in Scala?

Scala Thread is created by two steps:

Scala Thread & Scala Multithreading - File handling in Scala

How to Create Thread in Scala

a. Extending the Thread Class

Like discussed above, we can extend the Scala thread class or the runnable interface. Let’s try the first option.

scala> class Demo extends Thread{
| override def run(){
| println("Running")
| }
| }
defined class Demo
scala> var d=new Demo()
d: Demo = Thread[Thread-2,5,main]
scala> d.start()
scala> Running

Let’s Learn Scala String Interpolation – s String, f String 

b. Extending the Runnable Interface

Now, let’s try extending the interface runnable.

scala> class Demo extends Runnable{
| override def run(){
| println("Running")
| }
| }
defined class Demo
scala> var d=new Demo()
d: Demo = Demo@7d28cdcd
scala> var t=new Thread(d)
t: Thread = Thread[Thread-2,5,main]
scala> t.start()
scala> Running

Scala Thread Methods

We discussed the states in a Scala thread life-cycle. We can control thread flow to deal with these states using these methods. Let’s take a few examples.

Scala Thread Methods

Scala Thread Methods

a. sleep()

We can use this method to put a Scala thread to sleep for a number of milliseconds. Let’s take an example.

scala> class Demo extends Thread{
| override def run(){
| for(i<-1 to 7){
| println(i)
| Thread.sleep(400)
| }
| }
| }
defined class Demo
scala> var d1=new Demo()
d1: Demo = Thread[Thread-3,5,main]
scala> var d2=new Demo()
d2: Demo = Thread[Thread-4,5,main]
scala> d1.start(); d2.start()
scala> 1
1
2
2
3
3
4
4
5
5
6
6
7
7

This code prints numbers 1 to 7 in pairs. You can say it prints two 1s, then waits for 400 milliseconds, then prints two 2s, then waits 400 milliseconds, and so on.

Let’s read about Scala Variables with Examples

b. join()

join() lets us hold execution of the Scala thread currently running until it finishes executing. It waits for a thread to die before it can start more.

scala> class Demo extends Thread{
| override def run(){
| for(i<-1 to 7){
| println(i)
| Thread.sleep(400)
| }
| }
| }
defined class Demo
scala> var d1=new Demo(); var d2=new Demo(); var d3=new Demo()
d1: Demo = Thread[Thread-14,5,main]
d2: Demo = Thread[Thread-15,5,main]
d3: Demo = Thread[Thread-16,5,main]
scala> d1.start(); d1.join(); d2.start(); d3.start()
1
2
3
4
5
6
7
1
1
scala> 2
2
3
3
4
4
5
5
6
6
7
7

In this code, we call start() on d1, then join() on the same object. Then, we call start() on objects d2 and d3. This calls start() on d1; then join() executes it until it dies. And then, it calls start() on d2 and d3, and they run. To understand this, let’s take another example.

scala> class Demo extends Thread{
| override def run(){
| for(i<-0 to 5){
| println("1")
| Thread.sleep(400)
| }
| }
| }
defined class Demoscala> class Demo1 extends Thread{
| override def run(){
| println("Demo1")
| Thread.sleep(400)
| }
| }
defined class Demo1
scala> class Demo2 extends Thread{
| override def run(){
| println("Demo2")
| Thread.sleep(400)
| }
| }
defined class Demo2
scala> var d=new Demo(); var d1=new Demo1(); var d2=new Demo2()
d: Demo = Thread[Thread-29,5,main]
d1: Demo1 = Thread[Thread-30,5,main]
d2: Demo2 = Thread[Thread-31,5,main]
scala> d.start(); d.join(); d1.start(); d2.start()
1
1
1
1
1
1
Demo1
Demo2

In this example, we see in the REPL that 1 prints six times with an interval of 400 milliseconds. And then, Demo1 and Demo2 print simultaneously. This clears it. Let’s move to another method.

Let’s See Scala if-else Statements with Statements

c. setName() and getName()

The methods setName() and getName() let us set and get names of threads. Let’s see how.

scala> class Demo extends Thread{
| override def run(){
| for(i<-1 to 7){
| println(this.getName()+"- "+i)
| Thread.sleep(400)
| }
| }
| }
defined class Demo
scala> var d1=new Demo(); var d2=new Demo(); var d3=new Demo()
d1: Demo = Thread[Thread-35,5,main]
d2: Demo = Thread[Thread-36,5,main]
d3: Demo = Thread[Thread-37,5,main]
scala> d1.setName("One"); d2.setName("Two"); d3.setName("Three")
scala> d1.start(); d2.start(); d3.start()
Two- 1
One- 1
Three- 1
scala> Two- 2
One- 2
Three- 2
Two- 3
One- 3
Three- 3
Two- 4
One- 4
Three- 4
Two- 5
One- 5
Three- 5
Two- 6
One- 6
Three- 6
Two- 7
One- 7
Three- 7

This code runs the loop seven times for each Scala thread. This time, the order is Two, One, Three. The next time you try it, it may be One, Three, Two.

d. getPriority() and setPriority()

Using these methods, we can get and set the priority for a Scala thread. Let’s take an example.

scala> class Demo extends Thread{
| override def run(){
| for(i<-1 to 7){
| println(this.getName())
| println(this.getPriority())
| Thread.sleep(400)
| }
| }
| }
defined class Demo
scala> var d1=new Demo(); var d2=new Demo()
d1: Demo = Thread[Thread-47,5,main]
d2: Demo = Thread[Thread-48,5,main]
scala> d1.setName("One"); d2.setName("Two")
scala> d1.setPriority(Thread.MIN_PRIORITY); d2.setPriority(Thread.MAX_PRIORITY)
scala> d1.start(); d2.start()
scala> Two
One
10
1
Two
10
One
1
Two
10
One
1
Two
10
One
1
Two
10
One
1
Two
10
One
1
Two
10
One
1

This example tells us that the maximum priority is 10 and the minimum is 1.

Have a look – Scala Access Modifiers: Public, Private and Protected Members

e. task()

In this example, we create multiple Scala threads and use them to run multiple tasks. This lets us implement multitasking in Scala.

scala> class Demo extends Thread{
| override def run(){
| for(i<-1 to 4){
| println(i)
| Thread.sleep(400)
| }
| }
| def task(){
| for(i<-5 to 8){
| println(i)
| Thread.sleep(200)
| }
| }
| }
defined class Demo
scala> var d1=new Demo()
d1: Demo = Thread[Thread-51,5,main]
scala> d1.start(); d1.task()
1
5
6
2
7
8
3
scala> 4

f. More Methods

Other than and including what we just saw, we have the following methods:

1. Public static void sleep(long millis) throws InterruptedException- This puts the thread to sleep for a certain number of milliseconds.

2. Public final String getName()- This method gets the name of the thread.

3. Public final void setName(String name)- This method lets us set the name of the thread.

4. Public final int getPriority()- This method gives us the priority of the thread.

5. Public final void setPriority(int newPriority)- This method lets us set priority for a thread.

6. Public final boolean isAlive()- This tells us if a thread is alive (somewhere between states New and Terminated.

7. Public final void join() throws InterruptedException- Like we’ve seen, this method waits for the thread to die before starting another.

8. Public static void yield()- Yield compels the current running thread to give up control to other threads. It pauses the current thread.

9. Public Thread.State getState()- This returns the state of the thread. It lets us monitor system state.

So, this was all about Scala Thread & Multithreading. Hope you like our explanation.

Conclusion

Hence, in this Scala thread tutorial, we studied what is threading in Scala Programming. Along with this, we learned about File handling in Scala. Furthermore, if you have any query regarding Scala Threading, Feel free to ask in the comment section.
Related Topic-  What is Scala Trait Mixins
For reference

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

Leave a Reply

Your email address will not be published. Required fields are marked *