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,
- Process based multi-tasking
- 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 multi–tasking
- For example, we can execute/perform below programs (processes) simultaneously/parallelly in a Computer machine
- Listening songs from JioSaavn
- Downloading JDK version 1.8 from Oracle.com
- 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 multi–tasking 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 multi–tasking is also known as Multi–threading
- For example, we can execute below tasks simultaneously in a Program for video graphic game
- One thread is used to move characters from left-to-right and top-to-bottom
- At the same time, another thread can be used to align/produce sound for each characters
- And few other thread for sub–titles, etc.
- In the above list, each task is executed as a different thread within the same program/process
- Thread-based multi–tasking is best suitable at programmatic-level like Java programming language
4. Advantages of Multi-tasking :
- The main objective of multi–tasking 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 multi–threading)
5. Thread :
- Thread is a separate flow of execution in a program/process
- Threads are light–weight 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 inter–process communication faster and context–switching less expensive
5.1 Creating a Thread :
There are 2 ways to create a Thread in Java,
- By extending Thread class
- 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 Multi–threading. The important applications that can be developed using multi–threading 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 :
- https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
Happy Coding !!
Happy Learning !!