Multithreading in Java with example

Khurshid Md Anwar
3 min readJun 24, 2021

Threading in Java

When we want to run multiple programs or tasks simultaneously is known as multitasking. For example running VLC, Word, and Browser at a given time is an example of multitasking. Every program in memory is known as a process and each process has taken a single unit of time.

Multitasking can be achieved by

  • Multiprocessing is based on process and associated with the operating system level.
  • Multitasking is based on thread and is associated with the programmer.

What is Thread in java?

A thread is a subpart of a process which lightweight and it is the smallest sequence of a program or a process. Each thread has some priority and the higher the priority higher the chances of executing preference.

The life cycle of thread

The JVM controlled the life cycle of a thread and it has five states 1. New, 2. Runnable, 3. Running, 4. Non-Runnable (Blocked) and last 5. Terminated.

How to create a thread in java?

We can thread in java by two method

First, With Thread class extending and second with implementing Runnable interface.

Java provides predefine API to implement thread, a few of commonly used API as follows

  • thread () Create a new thread
  • Thread (Runnable target)
  • Thread (String name)
  • Thread (Runnable target, String name)
  • Thread (ThreadGroup g, Runnable target)

To create a thread we used two ways first by extending the Thread class and second by implementing the Runnable interface and this done by overriding the run() method. After then start() is called.

By implementing Runnable interface

public class RunnableInteface implements Runnable

{

public void run()

{

System.out.println(“thread is running.. by Runnable interface”);

}

public static void main(String args[])

{

RunnableInteface ob=new RunnableInteface();

Thread t1 =new Thread(ob);

t1.start();

}

}

Output

thread is running.. by Runnable interface

In the above program, we implement a Runnable interface. And override the run() method and then create an object of the class and that object passed in the Thread class constructor as Thread(ob).

By extending class Thread

public class ExtendingThread extends Thread

{

public void run()

{

System.out.println(“thread is running..by extending class Thread.”);

}

public static void main(String args[])

{

ExtendingThread t1=new ExtendingThread();

t1.start();

}

}

Output

thread is running..by extending class Thread.

Difference between Daemon and User Threads

The main difference between Daemon and User threads is, a Daemon thread is low priority thread and User thread is a high priority thread.

Daemon thread is used in the background supporting task and User thread is used in the foreground.

Daemon thread life depends on the User thread whereas User Thread are independent.

Creating of Daemon Thread

public class Daemon extends Thread {

@Override

public void run()

{

System.out.println(“User Thread or Non-Daemon Thread”);

}

}

class DaemonThread

{

public static void main(String[] args)

{

Daemon daemon = new Daemon();

daemon.start();

System.out.println(“Main Thread”);

System.out.println(“Is “ + daemon.getName() + “ a Daemon Thread: “ + daemon.isDaemon());

System.out.println(“Is “ + Thread.currentThread().getName() + “ a Daemon Thread: “

+ Thread.currentThread().isDaemon());

}

}

Few points about the thread

  1. A thread is the smallest unit of execution
  2. Multiple tasks execution is multitasking from the user perspective and from multithreading from an operating system perspective
  3. Thread is lightweight
  4. Thread is a part of the process and it is an instance of a program
  5. Java thread can be created either by class Thread or by interface Runnable
  6. in a thread a run() method override
  7. The start() method automatically invoke the run() method

More Java programs

Capitalize The First Character Of Each Word In A String
Java Program for BinarySearch
Java program for Selection Sort
Loops in Java — Java loop explained with examples
Java for loop
Array in Java

--

--