HTML Java

Java MultiThread


MultiThread using Lambda Expression

Example:

import java.lang.Thread;
public class LambdaThread {
public static void main(String args[]){
Runnable a = new Runnable(){
public void run(){
System.out.println("Hi A");
}
};
Runnable b = new Runnable(){
public void run(){
System.out.println("Hi B ");
}
};
Thread t1 =new Thread(a);
Thread t2 =new Thread(b);
t1.start();
t2.start();
}
}

Or also you can do like this

Example

import java.lang.Thread;
public class LambdaThread {
public static void main(String args[]){
Thread t1 =new Thread(() ->{
System.out.println("Hi A ");
});
Thread t2 =new Thread(() ->{
System.out.println("Hi B ");
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}

Output

Hi A
Hi B