Given an integer N, the task is to write Java Program to print the first N natural numbers in increasing order using two threads.
Prerequisite: Multithreading
Examples:
Input: N = 10
Output: 1 2 3 4 5 6 7 8 9 10Input: N = 18
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Approach: The idea is to create two threads and print even numbers with one thread and odd numbers with another thread. Below are the steps:
- Create two threads T1 and T2 using the below syntax, where T1 and T2 are used to print odd and even numbers respectively.
Thread T1 = new Thread(new Runnable() {
public void run() { mt.printEvenNumber(); }
});Thread T2 = new Thread(new Runnable() {
public void run() { mt.printOddNumber(); }
});where, printOddNumber() is used to print all the odd numbers till N, printEvenNumber() is used to print all the even numbers till N.
- Maintain a global counter variable and start both threads using the below function:
T1.start(); T2.start();
- If the counter is even in the Thread T1, then wait for the thread T2 to print that even number. Otherwise, print that odd number, increment the counter and notify to the Thread T2 using the function notify().
- If the counter is odd in the Thread T2, then wait for the thread T1 to print that odd number. Otherwise, print that even number, increment the counter and notify the Thread T1 using the function notify().
Below is the implementation of the above approach:
// Java program for the above approach
public class GFG {
// Starting counter
//will force all threads to update and use the latest copy of this counter, and not use locally cached copies
volatile static int counter = 1;
int limit;
GFG (int limit) {this.limit = limit;}
//function to print odd numbers
public synchronized void printOddNum () {
while(counter<=limit) {
if(counter%2 == 1) { //counter is odd, print it
// remove thread name and use System.out.print() to print in one line, as per the sample output format
System.out.println(Thread.currentThread().getName()+": "+counter);
counter++;
notifyAll();
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Function to print even numbers
public synchronized void printEvenNum () {
while (counter<=limit) {
if(counter%2 == 0) { //counter is even, print it
// remove thread name and use System.out.print() to print in one line, as per the sample output format
System.out.println(Thread.currentThread().getName()+": "+counter);
counter++;
notifyAll();
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Driver Code
public static void main(String[] args) {
GFG printer = new GFG (10);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
printer.printOddNum();
}
});
t1.setName("Odd"); // for clearer verification
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
printer.printEvenNum();
}
});
t2.setName("Even"); // for clearer verification
t1.start();
t2.start();
}
}
Output
Odd: 1 Even: 2 Odd: 3 Even: 4 Odd: 5 Even: 6 Odd: 7 Even: 8 Odd: 9 Even: 10
Time Complexity: O(N)
Auxiliary Space: O(1)