Multithreaded Programming
Multithreaded Programming
Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that
can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
Thus, multithreading is a specialized form of multitasking. Multithreading in Java is a process of executing multiple threads
simultaneously. A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading,
both are used to achieve multitasking. However, we use multithreading than multiprocessing because threads use a shared
memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes
less time than process.
Java Multithreading is mostly used in games, animation, etc.
Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking
can be achieved in two ways:
1. Process-based Multitasking (Multiprocessing)
2. Thread-based Multitasking (Multithreading)
Thread
A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of execution. Threads are
independent. If there occurs exception in one thread, it doesn't affect other threads. It uses a shared memory area.
As shown in the above figure, a thread is executed inside the process. There is context-switching between the threads.
There can be multiple processes inside the OS, and one process can have multiple threads.
Note: At a time one thread is executed only.
Thread class
Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and
perform operations on a thread. Thread class extends Object class and implements Runnable interface
Thread class:
Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object
class and implements Runnable interface.
Commonly used Constructors of Thread class:
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)
Commonly used methods of Thread class:
If you are not extending the Thread class, your class object would not be treated as a thread object. So, you
need to explicitly create Thread class object. We are passing the object of your class that implements
Runnable so that your class run() method may execute.
Example:
class MyThread extends Thread {
private String threadName;
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(500); // pause for 500ms
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
thread1.start();
thread2.start();
}
}
In this example:
Example:
class MyRunnable implements Runnable {
private String threadName;
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
thread1.start();
thread2.start();
}
}
Here, MyRunnable implements Runnable, and we pass instances of it to Thread objects. Both threads run concurrently.
@Override
public void run() {
System.out.println("Thread " + id + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println("Thread " + id + " has finished");
}
}
In this example:
• We create 5 threads using a for loop.
• Each thread prints a starting and finishing message with its ID.
Example:
class NewThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
Example:
class PriorityThread extends Thread {
public void run() {
System.out.println("Thread running with priority: " + this.getPriority());
}
}
t1.setPriority(Thread.MIN_PRIORITY); // 1
t2.setPriority(Thread.MAX_PRIORITY); // 10
t1.start();
t2.start();
}
}