# Print even and odd numbers in increasing order using two threads in Java

• Difficulty Level : Medium
• Last Updated : 27 Jan, 2022

Given an integer N, the task is to write Java Program to print the first N natural numbers in increasing order using two threads.

Examples:

Input: N = 10
Output: 1 2 3 4 5 6 7 8 9 10

Input: N = 18
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

public void run() { mt.printEvenNumber(); }
});

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

 `// Java program for the above approach`` ` `public` `class` `GFG {`` ` `    ``// Starting counter``    ``int` `counter = ``1``;`` ` `    ``static` `int` `N;`` ` `    ``// Function to print odd numbers``    ``public` `void` `printOddNumber()``    ``{``        ``synchronized` `(``this``)``        ``{``            ``// Print number till the N``            ``while` `(counter < N) {`` ` `                ``// If count is even then print``                ``while` `(counter % ``2` `== ``0``) {`` ` `                    ``// Exception handle``                    ``try` `{``                        ``wait();``                    ``}``                    ``catch` `(``                        ``InterruptedException e) {``                        ``e.printStackTrace();``                    ``}``                ``}`` ` `                ``// Print the number``                ``System.out.print(counter + ``" "``);`` ` `                ``// Increment counter``                ``counter++;`` ` `                ``// Notify to second thread``                ``notify();``            ``}``        ``}``    ``}`` ` `    ``// Function to print even numbers``    ``public` `void` `printEvenNumber()``    ``{``        ``synchronized` `(``this``)``        ``{``            ``// Print number till the N``            ``while` `(counter < N) {`` ` `                ``// If count is odd then print``                ``while` `(counter % ``2` `== ``1``) {`` ` `                    ``// Exception handle``                    ``try` `{``                        ``wait();``                    ``}``                    ``catch` `(``                        ``InterruptedException e) {``                        ``e.printStackTrace();``                    ``}``                ``}`` ` `                ``// Print the number``                ``System.out.print(``                    ``counter + ``" "``);`` ` `                ``// Increment counter``                ``counter++;`` ` `                ``// Notify to 2nd thread``                ``notify();``            ``}``        ``}``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Given Number N``        ``N = ``10``;`` ` `        ``// Create an object of class``        ``GFG mt = ``new` `GFG();`` ` `        ``// Create thread t1``        ``Thread t1 = ``new` `Thread(``new` `Runnable() {``            ``public` `void` `run()``            ``{``                ``mt.printEvenNumber();``            ``}``        ``});`` ` `        ``// Create thread t2``        ``Thread t2 = ``new` `Thread(``new` `Runnable() {``            ``public` `void` `run()``            ``{``                ``mt.printOddNumber();``            ``}``        ``});`` ` `        ``// Start both threads``        ``t1.start();``        ``t2.start();``    ``}``}`

Output:

```1 2 3 4 5 6 7 8 9 10
```

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up