What is Multi-threading in Java?

Multi-threading is a Java feature that allows concurrent execution of two or more parts of a program. Each part of such program is called a thread. So, threads are light-weight processes within a process.

Threads can be created by using two mechanisms :

  1. Extending the Thread class
  2. Implementing the Runnable Interface

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 for thread creation by extending the Thread class 
class DemoMultithreading 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"); 
        } 
    } 
} 
  
// Main Class 
public class Multithread 
{ 
    public static void main(String[] args) 
    { 
        int n = 8; // Number of threads 
        for (int i=0; i<8; i++) 
        { 
            DemoMultithreading object = new DemoMultithreading(); 
            object.start(); 
        } 
    } 
} 

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.

// Java code for thread creation by implementing the Runnable Interface 
class DemoMultithreading 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 Multithread 
{ 
    public static void main(String[] args) 
    { 
        int n = 8; // Number of threads 
        for (int i=0; i<8; i++) 
        { 
            Thread object = new Thread(new DemoMultithreading()); 
            object.start(); 
        } 
    } 
} 

 

Recent Posts

Explain Platform?

Any hardware or software environment in which a program runs, is known as a platform.…

5 years ago

What is ‘IS-A’ relationship in java?

'is a' relationship is also known as inheritance. We can implement 'is a' relationship or…

5 years ago

Explain Java Coding Standards for Constants?

Constants in java are created using static and final keywords. Constants contains only uppercase letters.…

5 years ago

Explain Java Coding Standards for variables ?

Variable names should start with small letters. Variable names should be nouns. Short meaningful names…

5 years ago

Explain Java Coding standards for Methods?

Method names should start with small letters. Method names are usually verbs If method contains…

5 years ago

Explain Java Coding standards for interfaces?

Interface should start with uppercase letters Interfaces names should be adjectives Example : Runnable, Serializable,…

5 years ago