How To Create A Thread In Java

Introduction

 
In this article, we discuss how to create a thread in Java and also discuss Multi-threading and Multi-tasking in Java.
 

Multi-threading

 
Multi-threading is a process of running/executing multiple threads simultaneously (at the same time).
 
A thread is basically a lightweight sub-process (the smallest unit of processing). The term multi-threading and multi-processing are both used to do multitasking. But we use multi-threading rather than multi-processing because the behavior of threads are to share a common memory area. They don't allocate separate memory so save memory, and context-switching between the threads takes less time than processes. Multi-threading is mostly used in games, animation, and so on.
 

Multi-tasking

 
Multi-tasking is a process of executing multiple tasks at a time. We use multi-tasking to utilize the CPU. It can be done by one of two ways;
  • Thread based multitasking
  • Process-based multitasking

1. Thread based

 
The features of Thread based are:
  • Threads share the same address space.
  • Threads are lightweight.
  • The cost of communication between threads within a process is low.

2. Process-based

 
The features of Process-based are:
  • The cost of communication between processes is high.
  • Moving from one process to another requires some time for saving and loading registers, memory, maps, updating lists, etcetera.
  • Each process has its own memory address, in other words, each process allocates a separate memory area.
  • It is heavyweight in nature.

How to create a thread in Java?

 
There are two ways to create a thread; they are:
  1. By extending the Thread class.
  2. By implementing the Runnable interface.

Thread class

 
This class provides constructors to create and perform operations on a thread. The Thread class extends the Object class and implements the Runnable interface.
 

Thread class Constructors

 
Some commonly used constructors of the thread class are:
  • Thread()
  • Thread(String sname)
  • Thread(Runnable run)
  • Thread(Runnable run, String sname)

Thread class Public methods

 
Some commonly used public methods of the Thread class are:
 
void yield()
       used to cause the currently executing thread object to temporarily pause and allow other threads to execute.
 
void suspend() 
       used to suspend the thread.
 
void resume()
       used to resume the suspended thread.
 
void stop() 
       used to stop the thread.
 
void run()
         used to start an action for a thread.
 
void start() 
         starts the execution of the thread.
 
void sleep()
        puts the currently executing thread to sleep.
 
void join()
        waits for a thread to die.
 
int getPrioprity()
        used to return the priority of a thread
 
int setPriority(int Priority)
        used to change the priority of the thread.
 
String getName()
        used to return the name of the thread.
 
void setName(String sname)
        used to change the name of the thread.
 
Thread currentThread()
        used to return the reference of the currently executing thread.
 
int getId()
        used to return the id of the thread.
 
Thread.State.getState()
        used to return the state of the thread.
 
boolean isAlive()
        used to test whether the thread is alive.
 

Runnable interface

 
This interface should be implemented by any class whose instances are intended to be executed by a thread. A Runnable interface has only one method named run().
 
public void run() 
      used to start action of thread.
 

Starting(executing) a thread.

 
The start() method is used to start a newly created thread. It performs the following tasks:
  • A new thread starts
  • The thread moves from the new state to the runnable state.
  • When the thread gets a chance to execute then its target run() method will run.

1. By extending the Thread class

  1. class MultithreadEx extends Thread {  
  2.  public void run() {  
  3.   System.out.println("thread is start running......");  
  4.  }  
  5.  public static void main(String[] args) {  
  6.   MultithreadEx m1 = new MultithreadEx();  
  7.   m1.start();  
  8.  }  
  9. }   
Output
 
Fig-1.jpg
 

2. By implementing the runnable interface

  1. class MultithreadEx1 implements Runnable {  
  2.  public void run() {  
  3.   System.out.println("Thread start running...");  
  4.  }  
  5.  public static void main(String args[]) {  
  6.   MultithreadEx1 mth1 = new MultithreadEx1();  
  7.   Thread th = new Thread(mth1);  
  8.   th.start();  
  9.  }  
  10. }   
Output
 
fIG-2.jpg


Similar Articles