What is multi-threading?How to achieve multi-threading in java?

Reading Time: 3 minutes
multi-threading
Multi-threading concept

Multi threading  is a process of executing multiple threads simultaneously.Multi threading don’t allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Multi-threading is the idea of multitasking into applications where you can distribute specific operations within a single application into individual threads. Each of the threads can run in parallel. It performs multi activities concurrently.

Multi Tasking

Multi-Tasking is the execution of multi-task simultaneously. Hence utilization of CPU can be best achieved with multi tasking.

multi-threading
multitasking

What is Thread?

 Thread is a lightweight sub-process, the smallest unit of processing. A thread, in the context of Java, is the path followed when executing a program. It is a sequence of nested executed statements or method calls that allow multiple activities within a single process. Sometimes, the thread is called execution context because it runs a code that works only within the context of the thread itself.

Life Cycle of Thread

thread life cycle
Life cycle of thread

Different ways to create thread in java multi-threading concepts

multi-threading
multi-threading in java
  • By extending Thread class
  • By implementing Runnable interface

Thread Class

Thread class constructors are used to create threads in java and its method helps in performing operations on it.

Constructors in Thread Class:

  1. Thread()
  2. Thread(String name)
  3. Thread(Runnable r)
  4. Thread(Runnable r,String name)

10 Common Methods in Thread Class:

MethodsDescriptions
public void run()used to perform action for a thread
public void start()starts the execution of the thread. JVM calls the run() method on the thread
public void sleep(long milliseconds)Causes the currently executing thread to sleep for given mileseconds
public void join()waits for a thread to die
public void suspend()used to suspend the thread
public void resume()used to resume the suspended thread
public void stop()used to stop the thread
public void interrupt()interrupts the thread
public int getPriority()returns the priority of the thread
public int setPriority(int priority)changes the priority of the thread
methods in Thread class

Example for creating thread in java by extending Thread class:

Step1: Override run( ) method available in Thread class.

public void run( )

Step2: Once the Thread object is created, the thread can be started by calling start() method, which executes a call to run( ) method.

void start( );
public class ThreadCreationExample extends Thread{  
    public void run(){  
        System.out.println("Thread is running");  
    }
    public static void main(String args[]){  
        ThreadCreationExample t1=new ThreadCreationExample();
        System.out.println("Thread creation by extending Thread class");
        t1.start();  
    }
}  

Runnable interface

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interfaces have only one method named run().

  1. public void run(): is used to perform action for a thread.

Example for creating thread in java by implementing Runnable interface

Step1: Implement a run() method provided by a Runnable interface.

public void run( )

Step2: Instantiate a Thread object using the following constructor.

Thread(Runnable classObjImpInterface, String threadName)

Step3: Once the Thread object is created, the thread can be started by calling start() method, which executes a call to run( ) method.

void start();
Simple Program to create thread by implementing Runnable interface
public class ThreadCreationExample implements Runnable{  
    public void run(){  
        System.out.println("thread is running");  
    }
    public static void main(String args[]){  
        ThreadCreationExample tceobj=new ThreadCreationExample();  
        Thread t1 =new Thread(tceobj);
        System.out.println("thread is running"); 
        t1.start();  
        System.out.println("thread is created by implementing Runnable interface"); 
    }  
}   

In Conclusion, multi-threading is a need of any programming for resource management.

Additionally to read in multi-threading in java:-

  • Synchronization
  • Deadlock in java
  • Inter-thread Communication

Further references in multi-threading:

https://www.javatpoint.com/multithreading-in-java

https://www.geeksforgeeks.org/multithreading-in-java/

Leave a Reply