Multithreading in Java

Multithreading in Java Tutorial with Examples

Any application can have multiple processes (instances). Each of this process can be assigned either as a single thread or multiple threads. We will see in this tutorial how to perform multiple tasks at the same time and also learn more about threads and synchronization between threads.

In this tutorial, we will learn:

What is Single Thread?

A single thread is basically a lightweight and the smallest unit of processing. Java uses threads by using a “Thread Class”.

There are two types of thread – user thread and daemon thread (daemon threads are used when we want to clean the application and are used in the background).

When an application first begins, user thread is created. Post that, we can create many user threads and daemon threads.

Single Thread Example:

  • package demotest;
  •  
  • public class GuruThread
  •  
  • {
  •  
  • public static void main(String[] args) {
  •  
  • System.out.println(“Single Thread”);
  •  
  • }
  •  
  • }

Advantages of single thread:

  • Reduces overhead in the application as single thread execute in the system
  • Also, it reduces the maintenance cost of the application.

What is Multithreading in Java?

MULTITHREADING in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other. Mulitple threads don’t allocate separate memory area, hence they save memory. Also, context switching between threads takes less time.

Example of Multi thread:

  • package demotest;
  •  
  • public class GuruThread1 implements Runnable
  •  
  • {
  •  
  • public static void main(String[] args) {
  •  
  • Thread guruThread1 = new Thread(“Guru1”);
  •  
  • Thread guruThread2 = new Thread(“Guru2”);
  •  
  • guruThread1.start();
  •  
  • guruThread2.start();
  •  
  • System.out.println(“Thread names are following:”);
  •  
  • System.out.println(guruThread1.getName());
  •  
  • System.out.println(guruThread2.getName());
  •  
  • }
  •  
  • @Override
  •  
  • public void run() {
  •  
  • }
  •  
  • }

Advantages of multithread:

  • The users are not blocked because threads are independent, and we can perform multiple operations at times
  • As such the threads are independent, the other threads won’t get affected if one thread meets an exception.

Thread Life Cycle in Java

The Lifecycle of a thread:

Thread Life Cycle

There are various stages of life cycle of thread as shown in above diagram:

  1. New
  2. Runnable
  3. Running
  4. Waiting
  5. Dead
  1. New: In this phase, the thread is created using class “Thread class”.It remains in this state till the program starts the thread. It is also known as born thread.
  2. Runnable: In this page, the instance of the thread is invoked with a start method. The thread control is given to scheduler to finish the execution. It depends on the scheduler, whether to run the thread.
  3. Running: When the thread starts executing, then the state is changed to “running” state. The scheduler selects one thread from the thread pool, and it starts executing in the application.
  4. Waiting: This is the state when a thread has to wait. As there multiple threads are running in the application, there is a need for synchronization between threads. Hence, one thread has to wait, till the other thread gets executed. Therefore, this state is referred as waiting state.
  5. Dead: This is the state when the thread is terminated. The thread is in running state and as soon as it completed processing it is in “dead state”.

Some of the commonly used methods for threads are:

Method
Description
start()This method starts the execution of the thread and JVM calls the run() method on the thread.
Sleep(int milliseconds)This method makes the thread sleep hence the thread’s execution will pause for milliseconds provided and after that, again the thread starts executing. This help in synchronization of the threads.
getName()It returns the name of the thread.
setPriority(int newpriority)It changes the priority of the thread.
yield ()It causes current thread on halt and other threads to execute.

Example: In this example we are going to create a thread and explore built-in methods available for threads.

top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon