Multithreading
Java Program to Create Thread by Implementing Runnable Interface
A Java program to create a thread by implementing the Runnable interface.
Problem Description
Write a Java program to create a thread by implementing the Runnable interface.
Code
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running...");
}
}
public class ImplementRunnable {
public static void main(String[] args) {
MyRunnable m1 = new MyRunnable();
Thread t1 = new Thread(m1);
t1.start();
}
}Output
Thread is running...Explanation
implements Runnable: Implement the Runnable interface.ThreadConstructor: Pass the Runnable instance to the Thread constructor.- Flexibility: Preferred over extending Thread class as it allows extending another class.
