springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Java 9 @SafeVarargs Annotation Enhancement Use Java Try with Resources Create Private Methods in Java 9 Java 9 Features with Examples Optional Class in Java BiConsumer Functional Interface in Java What is the BiFunction in Java 8 What is the BiPredicate in Java 8 What is Supplier Interface in Java 8 Consumer Functional Interface Example In Java 8
Core Java

Lambda Expression with Multithreading

Last Updated: 02-05-2022 Springjava

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?

What is a Java Bean Class? Java 8 Features Write Less Lines Of Code Lambda Expression In Java 8 Features

Leave your thought here

Your email address will not be published. Required fields are marked *

springjava_com
  • springjavateam@gmail.com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
Subscribe to Blog via Email
Subscribe
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us