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
- By extending the Thread class
- 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/
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.
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).
As you can notice we are getting an error here because in scala our class cannot inherit two classes.
Here we get to know what is the difference between Thread and Runnable which are extended by our class to create a thread.