Multithreading in Java

Multithreading In Java

Today in this article we will be discussing about what is multithreading java and how the thread is created.

Threads can be created by using two mechanisms 

  • Extending the Thread class 
  • Implementing the Runnable Interface

Let’s understand this in detail below.

Multithreading in Java

Understanding Thread


A thread of execution in computer science is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is usually part of the operating system.

Threads and processes are implemented differently in different operating systems, but in most cases, a thread is a component of a process.

Multiple threads can coexist within the same process and share resources like memory, whereas different processes do not.

Using two different tasks at the same time is referred to as multitasking. The thread is unit of a process.

What is Multithreading?

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of the CPU. Each part of such a program is called a thread. So, threads are light-weight processes within a process.

Threads can be created by using two mechanisms : 

  • Extending the Thread class 
  • Implementing the Runnable Interface
Multithreading in Java

Thread creation by extending the Thread class


We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.

Java Code

Run

class MultithreadingDemo extends Thread {
public void run()
{

try { // Displaying the thread that is running System.out.println("Thread " + Thread.currentThread().getId()+ " is running"); } catch (Exception e) { // Throwing an exception System.out.println("Exception is caught"); } } } public class Main { public static void main(String[] args) { int n = 8; // Number of threads for (int i = 0; i < n; i++) { MultithreadingDemo object = new MultithreadingDemo(); object.start(); } } }

Output

Thread 13 is running
Thread 19 is running
Thread 12 is running
Thread 16 is running
Thread 18 is running
Thread 14 is running
Thread 15 is running
Thread 17 is running

Thread creation by implementing the Runnable Interface

We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a Thread object and call start() method on this object.

Thread Class vs Runnable Interface 

  • If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes.
  • We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.
  • Using runnable will give you an object that can be shared amongst multiple threads. 
Run
// Java code for thread creation by implementing
// the Runnable Interface
class MultithreadingDemo implements Runnable {
public void run()
 {
  try {
  // Displaying the thread that is running
  System.out.println( "Thread " + Thread.currentThread().getId() + " is running");
 }
  catch (Exception e) {
  // Throwing an exception
  System.out.println("Exception is caught");
  }
 }
}

 // Main Class
 class Main {
 public static void main(String[] args)
 {
   int n = 8; // Number of threads
   for (int i = 0; i < n; i++) {
    Thread object = new Thread(new MultithreadingDemo());
   object.start();
  }
 }
}
Output
Thread 23 is running
Thread 20 is running
Thread 25 is running
Thread 24 is running
Thread 26 is running
Thread 22 is running
Thread 21 is running
Thread 27 is running

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription