Last updated on August 3rd, 2024
We can create Thread by implementing a Runnable interface and extending the Thread class.
Example:
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i <9; i++) {
System.out.println("Child Thread");
}
}
}
public class Test {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
for (int i = 0; i <9; i++) {
System.out.println("Main Thread");
}
}
}
Output:
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
- In this example we are creating a thread by creating an extra class i.e., “MyThread” is extending the Thread class.
- In this example creating an extra class and extending the Thread class is not a better choice if we need to extend one more class. There is not possible because Java does not support multiple inheritances.
- This example is increasing the length of the code of the program.
- In this example is generated two “.class” files are:
- MyThread.class
- Test. class
Example:
class MyRunnable implements Runnable {
public void run() {
for(int i=0; i<9;i++) {
System.out.println("Child Thread");
}
}
}
public class Test {
public static void main(String[] args) {
MyRunnable r=new MyRunnable();
Thread t=new Thread(r);
t.start();
for(int i=0; i<9;i++) {
System.out.println("Main Thread");
}
}
}
Output:
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
- In this example we are creating Thread by taking an extra class i.e., “MyRunnable” is implementing the Runnable Interface.
- This extra class is increasing the length of the code of this example.
- This example is generated by two “.class” files are:
- MyRunnable.class
- Test.class
Example:
public class Test {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <9; i++) {
System.out.println("Child Thread");
}
}
}).start();
for (int i = 0; i <9; i++) {
System.out.println("Main Thread");
}
}
}
Output:
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
- In the above example we are creating a Thread by using the Anonymous Inner class.
- In this we are not taking an extra to implement a Runnable interface. So the length of the code as compared to taking an extra class is concise.
- This example is generated by two “.class” file is:
- Test$1.class
- Test.class
Example:
public class Test {
public static void main(String[] args) {
Runnable r=()->{
for(int i=0; i<9;i++) {
System.out.println("Child Thread");
}
};
Thread t= new Thread(r);
t.start();
for(int i=0; i<9;i++) {
System.out.println("Main Thread");
}
}
}
Output:
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
- Runnable is a predefined Functional Interface then we call Lambda Expression for implementation of this Runnable interface. Then we don’t need to create an extra class for implementing the Runnable interface to create a thread in Java 8. Our code will be concise and an extra “.class” will not be generated.
- There is no need to create a MyRunnable class implementing the Runnable interface. Because we are invoking Lambda Expression for the implementation of the predefined Functional Interface.
- In this example we created a thread by using Lambda Expression that generated only one “.class” file i.e., “Test.class”.
- This example is making the length of the code concise as compared to creating a thread by taking an extra class to implement the Runnable interface, implementing an anonymous inner class to implement the Runnable interface and creating an extra class for implementing the Thread class.
Conclusion:
This topic is explained How to use different ways to create a thread? How to use Lambda Expression in Multithreading? Why don’t we need to create a separate class for implementing the Runnable interface?
Thanks for the good writeup. It in reality used to be a amusement account it. Look advanced to far introduced agreeable from you! However, how could we be in contact?