The source code to create a thread by extending the Thread class is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
// Scala program to create a thread
// by extending Thread class
class MyThread extends Thread {
override def run() {
var cnt: Int = 0;
while (cnt < 5) {
println("Thread is running...");
cnt = cnt + 1;
}
}
}
object Sample {
// Main method
def main(args: Array[String]) {
var thrd = new MyThread()
thrd.start()
}
}
Output:
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Explanation:
Here, we used an object-oriented approach to create the program. And, we created an object Sample.
We created a class MyThread by extending the Thread class and implement the run() method.
In the main() function, we created an object of the MyThread class and run the thread by calling the start() method.
Program/Source Code:
The source code to create a thread by extending the Thread class is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Output:
Explanation:
Here, we used an object-oriented approach to create the program. And, we created an object Sample.
We created a class MyThread by extending the Thread class and implement the run() method.
In the main() function, we created an object of the MyThread class and run the thread by calling the start() method.
need an explanation for this answer? contact us directly to get an explanation for this answer