Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading in Java

Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading | Priority In Java Multi Threading

To Get a better understanding of this topic read the below two post before continuing this post.

What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL] 

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface


Code:

public class ExampleCode 

{

public static void main(String[] args) 

{

Runnable hi=new Runnable() {

public void run()

{

for(int i=0;i<5;i++)

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

}

};

Runnable hello=new Runnable() {

public void run()

{

for(int i=0;i<5;i++)

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

}

};

Thread t1=new Thread(hi);

Thread t2=new Thread(hello);

t1.start();

t2.start();

}

}


If we take a look the above code it is similar to the example code of implementing Runnable interface code that we saw in the previous post.
But in this code we have modified the class that are declared in to Anonymous class. we can see that the classes are created inside the object creation of the Runnable. 

we can see that the Runnable Interface is a Functional Interface that means we can make use of the Lamda expression.

The below code has he changes to make it a lamda.

package firstcode;

public class ExampleCode 
{
public static void main(String[] args) 
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
}
}

Output :



Join Method in Multi Threading Programming :

Let's say we want to print bye after the hi and hello threads gets completed.
so if i add system out with bye as shown in the code below , the output is not like what we expected.

code :

public class ExampleCode 
{
public static void main(String[] args) 
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
System.out.println("Bye !");
}
}

 Output :



If you see the above output the bye is being printed before the hi and hello threads is completed.
The Reason is because,
We know that our main is also a thread.
we are creating two threads that are running but we need to note that our main thread is also been in running state so the bye is printed by main thread in parallel when hi and hello threads.

So what we need to do if we want to achieve what we want ?

Here comes the join() method.

code :

public class ExampleCode 
{
public static void main(String[] args) throws Exception
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Bye !");
}
}

Output :



The Join Method waits for the threads to complete its execution before going to the next step.
so now we got the bye after hi and hello threads are completed.

isAlive Method :

if we call this method it will return true if the thread is alive (i.e., in running state) else it will return false.

code:

public class ExampleCode 
{
public static void main(String[] args) throws Exception
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
System.out.println(t1.isAlive());
t1.join();
t2.join();
System.out.println(t1.isAlive());
System.out.println("Bye !");
}
}

Output:




we have checked isAlive() before thread t1 is completed(it returned true) and after t1 is completed(it returns false).


Continue Reading:

Priority in Java Multithreading | How to name a thread in java :

 
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts