WAP to print even and odd numbers using two threads
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.
Advantages of Java Multithreading
1) It doesn't block the user because threads are independent and you
can perform multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an
exception occurs in a single thread.
Multitasking
Multitasking is a process of executing multiple tasks simultaneously.
We use multitasking to utilize the CPU. Multitasking can be achieved
in two ways:
Process-based Multitasking (Multiprocessing)
Thread-based Multitasking (Multithreading)
1) Process-based Multitasking (Multiprocessing)
Each process has an address in memory. In other words, each
process allocates a separate memory area.
A process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another requires some time for
saving and loading registers, memory maps, updating lists, etc.
2) Thread-based Multitasking (Multithreading)
Threads share the same address space.
A thread is lightweight.
Cost of communication between the thread is low.
Note: At least one process is required for each thread.
What is Thread in java
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
public class Threaded {
private int counter = 1; // Starting counter
private static int N;
public void printOddNumber() {
synchronized (this) {
while (counter < N) {
while (counter % 2 == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(counter + " ");
counter++;
notify(); // Notify the other thread
}
}
}
public void printEvenNumber() {
synchronized (this) {
while (counter < N) {
while (counter % 2 == 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(counter + " ");
counter++;
notify(); // Notify the other thread
}
}
}
public static void main(String[] args) {
N = 10;
Threaded mt = new Threaded();
Thread t1 = new Thread(new Runnable() {
public void run() {
mt.printEvenNumber();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
mt.printOddNumber();
}
});
t1.start();
t2.start();
}
}
OUTPUT: