In  this article Multithreading in Java we give the information about multithreading is a process by which we can execute multiple threads simultaneously. This results in maximum utilization of CPU.

Multitasking:-

“Multitasking operating system is a multiprogram that can be used to perform more than one task at a time.”

We can listen to songs, use browser and even work in notepad at the same time on our computer. So this is what we call multitasking.

Multitasking

Benefits:

1- Timesharing

In this operating system, all the tasks are given appropriate time due to which the tasks are completed at the right time.

2- Reliability

It is reliable in which the user can execute more than one program simultaneously at a time.

Multiprocessing:

Multiprocessing Operating System is an operating system in which more than one CPU is used. Having more than one CPU increases the performance of the computer.

In a multiprocessing OS, the computer’s work is divided among these CPUs. Due to which the work is completed at a very fast pace.

Multiprocessing

Advantages:

In this, more work can be done at one time.

It has a separate processor to perform each task due to which the load is less.

Multithreading in Java:

In Java, multithreading is a process by which we can execute multiple threads simultaneously. This results in maximum utilization of CPU.

A thread is the smallest and lightweight sub-process of processing. Threads are independent from each other because their execution path is different. Even if an exception occurs in a thread, it does not affect other threads.

Multithreading is used to achieve multitasking. It is mostly used in creating games and animations.

Multithreading in Java

Advantage of Multithreading –

  1. It does not block the user because the threads are independent from each other.
  2. You can do many tasks at one time through this. Due to which time is saved.
  3. All the threads of a process share its resources (such as memory, data and files etc.). Due to which we do not need to allocate resources separately to threads.
  4. Multithreading reduces the idle time of CPU which improves the performance of the system.

What is Thread?

Thread is a lightweight process. Java supports multithreaded programming. A multithreaded program contains two or more parts that run simultaneously. Each part of such a program is called a thread.

The path of execution of each thread is different. And these are independent so that even if an exception occurs in one thread, it does not affect other threads.

There are two types of threads in it. First user thread and second daemon thread. (Daemon threads are used when we want to clean the application and they are used in the background.)

‘Thread Class’ is used to use threads in Java.

How to create a thread?

It can be created in two ways:-

Multithreading in Java can be implemented in two ways. In the first way you extend the thread class and in the second way the runnable interface is implemented.

  1. By extending Thread class
  2. By implementing Runnable interface

When you want to use other methods in the program besides run() method, then you can extend the thread class, but if you want to override only run() then you implement runnable interface.

Java threads class implementation:

The thread class provides the required methods to perform multithreading in Java. All the methods defined in the thread class are available with the default implementation.

Apart from this, you can override any method you want in your class.

run() method

No matter which method you use to perform multithreading, you need to override run() because the tasks to be executed by the thread are defined in the run() method.

This method is called by start() method. As soon as you call start() method through thread object, then run() method is called by start() method.

While implementing the thread class, you can also override other methods, but it is mandatory to override the run() method. Similarly, while implementing the runnable interface, it is mandatory to provide the definition of the run() method.

Steps to perform Multithreading using thread class

  1. First you extend the thread class
  2. After this you override the run() method in your class.
  3. After this, you create an object of your class (the class from which you implemented the thread class). While creating the object, the unique name of the thread can also be passed to the constructor.
  4. After this you call start method on that object.
  5. After this, run() method is called by start() method and the execution of your thread starts.

/* class thread

{

// many constructor

// different methods

run();

start();

sleap();

join();

getName() and setName()

etc.

}            */

// By extending Thread class

import java.lang.*;

class MultiDemo extends Thread

{

public void run()

{

System.out.println(“thread is running…”);

}

public static void main(String args[])

{

MultiDemo t1=new MultiDemo();

t1.start();

}

}

Output:- thread is running…

Java runnable interface implementation:

Another way to perform multithreading in Java is to implement runnable interface. runnable is a functional interface, in which only one run() method is defined, the definition of which you provide in your class.

Steps to perform multithreading using runnable interface

  1. First of all you create a class which implements the run() method and create that class.
  2. After this you create an object of thread class and pass the name of the class and thread implementing the runnable interface to the constructor as argument.
  3. After this, the object method of the thread class is called and the execution of the thread starts.

//By implementing the runnable interface

class MultiDemo implements Runnable

{

public void run()

{

System.out.println(“thread is running…”);

}

public static void main(String args[])

{

MultiDemo m1=new MultiDemo();

Thread t1 =new Thread(m1);

t1.start();

}

}

Output:- thread is running…

Difference between Thread class and runnable interface

  1. If we extend the thread class then our class cannot extend other classes. Because Java does not support multiple inheritance. But if we implement runnable interface then our class can extend other base classes.
  2. We can achieve the basic functionality of a thread by extending the thread class. Because it provides in-built methods like:- yield(), interrupt() etc. Whereas this method is not available in the runnable interface.

Thread methods in Java:

Below you are given some important thread methods present in thread class:-

Method Name Description
start() It is used to start the thread.
run() It is used to run the thread.
sleep() Suspends the thread for a period of time.
join() Waits for the thread to end.
getPriority() Returns the priority of the thread.
setPriority() It changes the priority of the thread.
getName() Returns the name of the thread.
setName() Changes the name of the thread.
isAlive() Checks whether the thread is alive or not.
yield() Pauses the currently executing thread object and executes other threads.
// Show the current Thread Name also change Thread name
import java.lang.*;
class Gth
{
public static void main(String []args)
{
System.out.println(“Hello”);
System.out.println(Thread.currentThread().getName());
Thread.currentThread().setName(“Rajveer”);
System.out.println(Thread.currentThread().getName());
System.out.println(10/0);
}
}

Programs:

1. Performing single task from single thread.

// By extending Thread class

import java.lang.*;

class MultiDemo extends Thread

{

public void run()

{

System.out.println(“thread is running…”);

}

public static void main(String args[])

{

MultiDemo t1=new MultiDemo();

t1.start();

}

}

Output:- thread is running…

2. Performing single task from multiple thread.

// By extending Thread class

import java.lang.*;

class MultiDemo extends Thread

{

public void run()

{

System.out.println(“thread is running…”);

}

public static void main(String args[])

{

MultiDemo t1=new MultiDemo();

t1.start();

MultiDemo t2=new MultiDemo();

t2.start();

}

}

Output:-

thread is running…

thread is running…

3. Performing multiple task from single thread. ( no work)

4. Performing multiple task from multiple thread.

import java.lang.*;
class MyThread1 extends Thread
{
public void run()
{
int a=10, b=20;
System.out.println(“Addition: “+(a+b));
}
}
class MyThread2 extends Thread
{
public void run()
{
int l=4,b=2;
System.out.println(“Area of rectangle: “+(l*b));
}
}
public class MultiThread
{
public static void main(String [] args)
{
MyThread1 t1=new MyThread1();
t1.start();
MyThread2 t2=new MyThread2();
t2.start();
}
}

Output:-

Compile: javac MultiThread.java

Run: java MultiThread
Addition: 30
Area of rectangle: 8

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *