What Is Multithreading | Multithreading Programs In Java
What is a Thread
# A thread is the path of execution within the group of A processes that can use multiple threads.
What is Multithreading
In multithreading, a simple program is broken into parts and every part is executed simultaneously. The parts can be defined in a way so that it does not affect the running of other parts. It expands performing various tasks where the application is partitioned into activities called Threads.
So overall multithreading improves the responsiveness of a System.
Need of Multithreading-
# Multithreading helps us to improve the responsiveness of a System.
For example in a web browser, if everything runs in a single thread, then the system will be completely unresponsive/slow whenever data is being fetched to display.
How to Write Multithreaded Programs in Java
# We can create Threads in java using the following ways
# Extending the Thread Class
# Implementing the Runnable Interface
# Implementing the Callable Interface
# By using the Runnable
Read Also:- Read & Write Data Using Apache POI — JAVA
Extending the Thread Class
In order to do this, we create a class and then extend the Thread class.
For creating a Thread, we need to create an instance of the Test class. And then for starting the thread we use Start() function.
class Test extends Thread {
@Override
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println(Thread.currentThread().getName() + “: ” + i); // it will return the current thread name
}}
}
public class ThreadClassDemo {
public static void main(String[] args) {
Thread t1 = new Test();
Thread t2 = new Test();
Thread t3 = new Test();
t1.start();
t2.start();
t3.start();
}
}
Implementing the Runnable Interface-
In order to do this, we create a class and then implement the Runnable Interface. In order to create a Thread, first we need to create an Instance of RunnableDemo Class which implements the Runnable Interface.
class RunnableDemo implements Runnable{
@Override
public void run() {
for (int i = 0; i <= 4; i++) {
System.out.println(Thread.currentThread().getName() + “: ” + i);
}
}
}
public class RunnableInterfaceDemo {
public static void main(String[] args) {
Runnable run = new RunnableDemo();
Thread thread1 = new Thread(run);
Thread thread2= new Thread(run);
Thread thread3= new Thread(run);
thread1 .start();
thread2 .start();
thread3 .start();
}
}
On Running the Above code, the sequence of the output will change every time the code is run.
Read Also:- Java Script Executor in Selenium Test Automation
Runnable Interface in Java 8
In Java 8, the Runnable Interface is used to achive Multithreading.
public class RunnableFunctionalInterfaceDemo {
public static void main(String[] args) {
Runnable r = () -> {
for (int i = 0; i <= 4; i++) {
System.out.println(Thread.currentThread().getName() + “: ” + i);}};
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();}}
Originally published at https://www.devstringx.com on Feb 07, 2022.