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

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

Before Reading this post if you have not seen the previous post on Multi Threading and why we need it ,first take a look at it with the below link and continue this post.

https://iamafutureprogrammer.blogspot.com/2021/11/what-is-multi-threading-why-do-we-need.html


Below is a simple java code that will print Hi and Hello 5 times by calling the method.

Code:

class SayHi

{

public void say()

{

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

{

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

try 

{

Thread.sleep(1000);

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

}

}

}


class SayHello

{

public void say()

{

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

{

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

try 

{

Thread.sleep(1000);

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

}

}

}


public class ExampleCode 

{

public static void main(String[] args) 

{

SayHi hi=new SayHi();

SayHello hello=new SayHello();

hi.say();

hello.say();

}

}


Output:



In the above java code we can see that after the hi.say() get executed the hello.say() starts its execution.
Note : the main method is by default a thread.

Sleep:

If we want to make our thread to suspend the execution for some time , then we can make use of Thread.sleep(1000) method.

we need to pass the milli seconds for which the execution needs to be suspended . e.g., 1000 means 1 seconds. So the it will print and wait 1 second for each.


Now our aim is to make the above program as multi threaded.
i.e., we are going to make both the hi.say() and hello.say() to execute in parallel.

Method 1 : Extending Thread Class 

To do that we can extend the thread class as follows.

package firstcode;

class SayHi extends Thread
{
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);}
}
}
}

class SayHello extends Thread
{
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);}
}
}
}

public class ExampleCode 
{
public static void main(String[] args) 
{
SayHi hi=new SayHi();
SayHello hello=new SayHello();
hi.start();
hello.start();
}
}

Output:


We need to extend the Thread Class to the classes for which we need to achieve threading.

we need to rename the method name as run() .
in spite of calling the method name we need to call a method called as start() .

the start method will invoke the run() method internally.

From the output we can see that the both the methods are executed parallely.

Note : the output we got above is not constant . see the below output for the same code when i ran again.

 You can see that they are executed in parallel but the order is different . 
This is because in Threading both the SayHi.start() and SayHello.start() will execute at the same time (may be  some millisecond difference) so the OS scheduler will decide what need to done first if both threads reaches the scheduler at the same time. the scheduler then randomly decides which one should be executed first. that's why we have a different output order.

Method 2 : Implementing Runnable Interface

Why we need to go for Runnable Interface when Thread Class is already there?

We know multiple inheritance is not supported in Java.
imagine we need a class A that needed to be extended to SayHi class above. at the same time we need to extend Thread class to.
so we need to do as below.

class SayHi extends A, Thread

But its not possible in java since it is a multiple inheritance.

So to over come this problem we make use of a interface called as Runnable.

Below is the same example we seen above now modified to use Runnable Interface.


Code:

package firstcode;

class SayHi implements 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);}
}
}
}

class SayHello implements 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);}
}
}
}

public class ExampleCode 
{
public static void main(String[] args) 
{
Runnable hi=new SayHi();
Runnable hello=new SayHello();
Thread t1=new Thread(hi);
Thread t2=new Thread(hello);
t1.start();
t2.start();
}
}

Output:



Runnable is a Functional Interface because it only has a single method called run().

Runnable has no start() method so we cannot call start() method directly . so we are making using of Thread class and taking the Runnable objects to it and then we are using the object of Thread class to call the start() method.

 
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts