w3resource

Java thread Programming - Find and Print Even-Odd Numbers with Threads

Java Thread: Exercise-2 with Solution

Write a Java program that creates two threads to find and print even and odd numbers from 1 to 20.

Sample Solution:

Java Code:

public class Find_Even_Odd_Number {
  private static final int MAX_NUMBER = 20;
  private static Object lock = new Object();
  private static boolean isEvenTurn = true;

  public static void main(String[] args) {
    Thread evenThread = new Thread(() -> {
      for (int i = 2; i <= MAX_NUMBER; i += 2) {
        synchronized(lock) {
          while (!isEvenTurn) {
            try {
              lock.wait();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
          System.out.println("Even Number from evenThread: " + i);
          isEvenTurn = false;
          lock.notify();
        }
      }
    });

    Thread oddThread = new Thread(() -> {
      for (int i = 1; i <= MAX_NUMBER; i += 2) {
        synchronized(lock) {
          while (isEvenTurn) {
            try {
              lock.wait();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
          System.out.println("Odd Number from oddThread: " + i);
          isEvenTurn = true;
          lock.notify();
        }
      }
    });

    evenThread.start();
    oddThread.start();
  }
}

Sample Output:

 Even Number from evenThread: 2
Odd Number from oddThread: 1
Even Number from evenThread: 4
Odd Number from oddThread: 3
Even Number from evenThread: 6
Odd Number from oddThread: 5
Even Number from evenThread: 8
Odd Number from oddThread: 7
Even Number from evenThread: 10
Odd Number from oddThread: 9
Even Number from evenThread: 12
Odd Number from oddThread: 11
Even Number from evenThread: 14
Odd Number from oddThread: 13
Even Number from evenThread: 16
Odd Number from oddThread: 15
Even Number from evenThread: 18
Odd Number from oddThread: 17
Even Number from evenThread: 20
Odd Number from oddThread: 19

Java Thread Exercises: Find and Print Even-Odd Numbers with Threads

Explanation:

The evenThread prints even numbers. It starts with 2 and increments by 2 in each iteration until it reaches MAX_NUMBER (which is 20 in this case). It uses a synchronized block with a while loop to check if it's its turn to print. If not, it waits by calling lock.wait(). When it's its turn, it prints the even number and notifies the oddThread by calling lock.notify().

The oddThread prints odd numbers. It starts with 1 and increments by 2 in each iteration until it reaches MAX_NUMBER. Similar to the evenThread, it uses a synchronized block with a while loop to check if it's its turn to print. If not, it waits by calling lock.wait(). When it's its turn, it prints the odd number and notifies the evenThread by calling lock.notify().

Flowchart:

Flowchart: Java Thread  Exercises - Find and Print Even-Odd Numbers with Threads.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Create a Basic Thread to Print "Hello, World!".
Next: Multithreaded Java Program: Sorting an Array of Integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/thread/java-thread-exercise-2.php