Monday, January 3, 2022

Multithreading in Java

Multithreading provider ability to execute multiple different path of code in same time

Normally java run into only one thread, but we have the ability to break off into multiple thread and do multiple things at once.


There are two main way to create thread.

1. Class extends Thread class, we need to override the run method from thread class, which is extends from Runnable interface.

 public class MyThread extends Thread {


@Override
public void run(){
for(int i = 0; i < 5; i++){
System.out.println("Count : " + i + " Thread name : " + Thread.currentThread());
}
}
}

public class ThreadTester {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}

Here the test file to run the thread and we are printing currently running thread. Below console log,




2. Implements Runnable interface, override run method from Runnable interface. While using implement we need to create Thread instance explicitly to call start() or run().

public class MyRunnable implements Runnable {
@Override
public void run() {
for(int i = 0; i < 5; i++){
System.out.println("Thread is running with: " + i + " " + Thread.currentThread());
}
}
}
public class ThreadTester {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
Above tester class we are instantiate thread object with name of MyRunnable class. 
And console log is below

If you notice we are override run() method from Runnable interface, but we called start()
from Thread class.
What will happen if we call run() instead of start(). Let me explain about start() and run().

Start():
    Calling start() will create a new thread from existing thread then run() will be execute, 
and its enables multithreading.
From above two coding example, we are seeing that Tread-0 is created from main thread.
[Thread-0,5,main]

run(): Calling run() will not create new thread, it execute as normal method with existing 
thread, no multithreading will be enabled.
let see example of calling run()
 
public class ThreadTester {
    public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.run();
}
}
Let see from output, 

From above console the run method is called as a method in main thread [main,5,main].

Now let see how the multiple thread running in java, here I will create two object of 
MyThread class and call start() of created instance.
public class ThreadTester {
public static void main(String[] args) {
MyThread myThread = new MyThread();
MyThread myThread1 = new MyThread();
myThread.start();
myThread1.start();
}
}
From below console, we are able to see the both thread executed 5times and creating new thread,
 but execution order is not maintained in jvm.
order of execution is changed from highlighted log line.

We can ensure that currently running thread to complete its action until we can hold the
execution of other thread by calling join()
public class ThreadTester {
public static void main(String[] args) {
MyThread myThread = new MyThread();
MyThread myThread1 = new MyThread();
myThread.start();
try {
myThread.join(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myThread1.start();
}
}
Here I called myThread.join(mills), this will ensure that myThread is complete this action 
and allows myThread1.start() to run.
Below logs we can see Thread-0 is executed before Thread-1 start().


No comments:

Post a Comment