Skip to content

Multithreading In Java Programming Language

In this article, you will learn Multithreading in Java Programming Language. It will cover thread life cycle, creating a thread, thread priority.

Multithreading may be defined as the process of executing two or more parts(multiple threads) of a java program simultaneously so as to manage multiple tasks. It is also known as thread-based multitasking.

A thread is the smallest unit of a process. All the threads in a process share common processing resources(common memory). Therefore, they are treated as lightweight processing units which help in achieving multitasking.

Another way to achieve multitasking is multiprocessing which is also known as process-based multitasking. In a nutshell, Multitasking is achieved through multithreading and multiprocessing. But in this article we will focus only on multithreading.

In a multithreaded program, all the threads are independent of each other which means, the occurrence of an exception in a thread doesn’t affect execution of the other threads in the same process.

thread-in-java

THREAD LIFE CYCLE

In java, there are several stages which a thread undergoes in its lifecycle and is managed by the Thread Scheduler, a part of Java Virtual Machine(JVM). We have shown below a pictorial representation of different states a thread acquires while going through different stages in its lifecycle.

multithreading-in-java

(1) New- When an instance of thread class is created, the thread is said to be born and begins its life in the New state.It stays in this state until the start() method is invoked to perform further operations.

(2) Runnable- The thread is said to be in Runnable state when the start() method has been invoked but it has not been selected to be in the running state by the thread scheduler.

(3) Running- After the thread has been selected by the thread scheduler, it is said to be in the Running state.

(4) Waiting- In this state, a thread waits for another thread to perform a particular task. When the task is finished, the another thread responds back to the waiting thread to continue performing its task. In other words, the waiting thread comes back to the running state.

(5) Terminated/ Dead- When a running thread has finished its task, it reaches the terminated state or dead state.

CREATING A THREAD

A thread can be created in the following two ways:

  1. Either by extending Thread Class, or
  2. By implementing Runnable Interface.

[1] BY EXTENDING THREAD CLASS

The Thread Class contains the following constructors and methods.

Constructors:

  • Thread()
  • Thread(String name)
  • Thread(Runnable rn)
  • Thread(Runnable rn, String name)
  • Thread(ThreadGroup group, Runnable rn)
  • Thread(ThreadGroup group, Runnable rn, String name)
  • Thread(ThreadGroup group, String name)
  • Thread(ThreadGroup group, Runnable rn, String name, long stackSize)

Methods:

  • public void run()
  • public void start()
  • static void sleep(long millis)
  • void checkAccess()
  • protected Object clone()
  • static Thread currentThread()
  • static void dumpStack()
  • long getId()
  • String getName()
  • void setName(String name)
  • int getPriority()
  • void setPriority(int newPriority)
  • Thread.State getState()
  • ThreadGroup getThreadGroup()
  • void interrupt()
  • static boolean interrupted()
  • boolean isAlive()
  • boolean isInterrupted()
  • void join()
  • void join(long millis)
  • static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)
  • String toString()
  • static void yield()
  • boolean isDaemon()
  • StackTraceElement[] getStackTrace()
  • void setDaemon(boolean on)
  • boolean isDaemon()

Example:

class ThreadEx extends Thread {
public void run() {
System.out.println(“Thread is in Running State”) ;
}
public static void main(String args[]) {
ThreadEx tobj = new ThreadEx() ;
tobj.start() ;
}
}

Output:

Thread is in Running State

[2] BY IMPLEMENTING RUNNABLE INTERFACE

The Runnable Interface contains only a single method which is shown below.

Method:

  • public void run()

Example:

class ThreadEx implements Runnable {
public void run() {
System.out.println(“Thread is in Running State”) ;
}

public static void main(String args[]) {
ThreadEx tex = new ThreadEx() ;
Thread tobj = new Thread(tex) ;
tobj.start() ;
}
}

Output:

Thread is in Running State

THREAD PRIORITY

Thread Priority plays an important role in a multithreaded java program. Threads having priority higher than other threads are executed first which is managed by the thread scheduler. Numbers between 1 to 10 are used to represent priorities of threads.

The Thread Class consists of following constant fields.

  1. static int MIX_PRIORITY- It represents the minimum priority of a thread and its value is 1
  2. static int MAX_PRIORITY- It represents the maximum priority of a thread and its value is 10.
  3. static int NORM_PRIORITY- It represents the default priority of a thread and its value is 5.

Example:

class ThreadPriorityEx extends Thread {
public void run() {
System.out.println(“Name of the running thread is :” +Thread.currentThread().getName()) ;
System.out.println(“Priority of the running thread is :” +Thread.currentThread().getPriority()) ;
}

public static void main(String args[]) {

ThreadPriorityEx tpe1 = new ThreadPriorityEx() ;
ThreadPriorityEx tpe2 = new ThreadPriorityEx() ;

tpe1.setPriority(Thread.MIN_PRIORITY) ;
tpe2.setPriority(Thread.MAX_PRIORITY) ;

tpe1.start() ;
tpe2.start() ;
}
}

Output:

Name of the running thread is : Thread-0
Name of the running thread is : Thread-1
Priority of the running thread is : 1
Priority of the running thread is : 10

Also Read:- Generic Methods and Classes in Java Programming Language

We have provided you the description on Multithreading in Java Programming Language. Hope you like this article. For more updates and related information, stay connected to our blogs on Java Programming Language.

Facebook
Twitter
LinkedIn
Pinterest