Java – Introduction to Thread

In this article, we will discuss the concepts of Thread in Java

1. Task and Multi-tasking :

Before understanding Thread, first we will try to understand task and multi-tasking in general

1.1 Task :

  • a piece of work to be done or
  • a piece of work to be executed in a computer/machine

1.2 Multi-tasking :

  • when multiple tasks to be done simultaneously or
  • when several tasks to be executed in a computer/machine simultaneously or parallelly

There are 2 types of multi-tasking,

  1. Process based multi-tasking
  2. Thread based multi-tasking

2. Process based Multi-tasking :

  • Executing several tasks simultaneously or parallelly where each task is independent separate program/process is known as Process-based multitasking
  • For example, we can execute/perform below programs (processes) simultaneously/parallelly in a Computer machine
    1. Listening songs from JioSaavn
    2. Downloading JDK version 1.8 from Oracle.com
    3. Preparing Microsoft PowerPoint presentation
  • In the above list, each task is executed as a separate program/process and they’re independent of each other
  • Process-based multitasking is implemented at Operating System level

3. Thread based Multi-tasking :

  • Executing several tasks simultaneously or parallelly where each task is independent parts of the same program/process and each independent parts is known as Threads
  • Thread-based multitasking is also known as Multithreading
  • For example, we can execute below tasks simultaneously in a Program for video graphic game
    1. One thread is used to move characters from left-to-right and top-to-bottom
    2. At the same time, another thread can be used to align/produce sound for each characters
    3. And few other thread for subtitles, etc.
  • In the above list, each task is executed as a different thread within the same program/process
  • Thread-based multitasking is best suitable at programmatic-level like Java programming language

4. Advantages of Multi-tasking :

  • The main objective of multitasking is to,
    • reduce response-time of the system
    • improve performance irrespective of whether it is Process-based multi-tasking or Thread-based multi-tasking (or multithreading)

5. Thread :

  • Thread is a separate flow of execution in a program/process
  • Threads are lightweight processes or smallest unit of work in a process
  • Creating a Thread or spawning a new Thread means allocating a separate independent flow of execution in a process
  • So, in a process we can create as many as Threads required for the process/program or in other way a process can contain multiple threads independent of each other
  • Multiple threads of the same process share memory address of that process allowing interprocess communication faster and contextswitching less expensive

5.1 Creating a Thread :

There are 2 ways to create a Thread in Java,

  1. By extending Thread class
  2. By implementing Runnable interface

MyThread.java

package in.bench.resources.threads;

public class MyThread extends Thread {

	@Override
	public void run() {

		// work for new thread
		for(int i=0; i<10; i++) {
			System.out.println("value = " + i);
		}
	}
}

MyRunnable.java

package in.bench.resources.threads;

public class MyRunnable implements Runnable {

	@Override
	public void run() {

		// work for new thread
		for(int i=0; i<10; i++) {
			System.out.println("value = " + i);
		}
	}
}

6. Multi-threading :

When there are 2 or more threads in a process then it is known as Multithreading. The important applications that can be developed using multithreading are,

  • multi-media graphics
  • animation
  • video games
  • web-servers and application-servers, etc.

MultiThreading.java

package in.bench.resources.threads;

// main Thread implementation
public class MultiThreading {

	public static void main(String[] args) {

		// Thread 1 execution
		MyThread1 thread1 = new MyThread1();
		thread1.start();


		// Thread 2 execution
		MyThread2 thread2 = new MyThread2();
		thread2.start();
	}
}


// Thread 1 implementation
class MyThread1 extends Thread {

	@Override
	public void run() {

		// work for new thread-1
		for(int i=0; i<10; i++) {
			System.out.println("value = " + i);
		}
	}
}


//Thread 2 implementation
class MyThread2 extends Thread {

	@Override
	public void run() {

		// work for new thread-2
		for(int i=10; i<20; i++) {
			System.out.println("value = " + i);
		}
	}
}

6.1 Multi-threading with Java :

  • Developing multi-threaded application in java is easy & flexible as Java provides many rich APIs like Thread, Runnable and ThreadGroup, etc.

In the following article, we will discuss different ways to create a Thread in detail

Related Articles :

  • Java – Introduction to Threads
  • Java – Different ways to create/spawn a Thread
  • Java – Thread Scheduler
  • Java – Important methods and constructors of a Thread class
  • Java – Difference between start() and run() methods of Thread class
  • Java – Importance of start() method in Thread class
  • Java – Overloading run() method of Thread class
  • Java – Effects of not overriding run() method of Thread class
  • Java – Setting and getting name of a Thread
  • Java – Thread priorities

References :

Happy Coding !!
Happy Learning !!

Java – Different ways to create/spawn a Thread