Let’s find out the difference between Thread and Runnable

Computer code on screens
Reading Time: 2 minutes

Multithreading is a process where we are uses multiple threads (threads are the sub-processes, they are lightweight and occupy less space) to perform various tasks simultaneously. Multithreading is used to achieve multitasking. There are two ways by which we can create threads in SCALA

  1. By extending the Thread class
  2. By extending the Runnable interface

Now there is a question that what is the difference between these two ways. If you want to know then this blog is for you. If you want to know about multithreading you can check these sites: https://blog.knoldus.com/things-to-know-about-multithreading-in-scala/

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

Thread

Difference between Thread and Runnable which we used while creating a threads

Let us see some basic differences between them.

1. “Thread” which we used to extend for creating a thread is actually a class whereas “Runnable” is a functional interface.

threads using Runnable interface
threads using thread class

2. Thread class has multiple methods ( start(), run(), setName(), getName() etc.) inside it but Runnable only has one abstract method.

3. We cannot inherit another class when we are extending the Thread class (because only one class can be inherited in scala) but we can extend another class with our class when we extend Runnable (because it is a functional interface).

threads error

As you can notice we are getting an error here because in scala our class cannot inherit two classes.

threads

Conclusion

Here we get to know what is the difference between Thread and Runnable which are extended by our class to create a thread.

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.