Backend Development 5 min read

Printing Odd and Even Numbers with Two Threads in Java Using synchronized/wait‑notify and Semaphore

This article explains how to print numbers from 1 to 100 with two Java threads—one handling odd numbers and the other even numbers—by first using a synchronized block with wait/notify and then by employing Semaphore for thread coordination.

IT Services Circle
IT Services Circle
IT Services Circle
Printing Odd and Even Numbers with Two Threads in Java Using synchronized/wait‑notify and Semaphore

In this article, the author presents a Java solution for printing numbers 1‑100 using two threads—one for odd numbers and one for even numbers—demonstrating both a synchronized + wait/notify approach and a Semaphore approach.

synchronized + wait/notify implementation

A ParityPrinter class is defined with fields max , count , and a lock object. The printOdd and printEven methods delegate to a private print method that uses a synchronized(lock) block, a while loop to wait for the correct turn, lock.wait() , printing, and lock.notify() .

public class ParityPrinter {
    private final int max;
    private int count = 1; // start from 1
    private final Object lock = new Object();

    public ParityPrinter(int max) {
        this.max = max;
    }

    public void printOdd() {
        print(true);
    }

    public void printEven() {
        print(false);
    }

    private void print(boolean isOdd) {
        for (int i = 1; i <= max; i += 2) {
            synchronized (lock) {
                while (isOdd == (count % 2 == 0)) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
                System.out.println(Thread.currentThread().getName() + " : " + count++);
                lock.notify();
            }
        }
    }
}

// Thread creation
ParityPrinter printer = new ParityPrinter(100);
Thread t1 = new Thread(printer::printOdd, "Odd");
Thread t2 = new Thread(printer::printEven, "Even");
t1.start();
t2.start();

The program outputs alternating lines such as Odd : 1 , Even : 2 , continuing up to Odd : 99 and Even : 100 .

Semaphore implementation

Using two Semaphore instances— oddSemaphore (initial permits 1) and evenSemaphore (initial permits 0)—the same printing logic is achieved. Each thread acquires its semaphore, prints the current number, then releases the other semaphore, ensuring strict alternation.

public class ParityPrinter {
    private final int max;
    private int count = 1;
    private final Semaphore oddSemaphore = new Semaphore(1); // odd thread starts
    private final Semaphore evenSemaphore = new Semaphore(0);

    public ParityPrinter(int max) {
        this.max = max;
    }

    public void printOdd() {
        print(oddSemaphore, evenSemaphore);
    }

    public void printEven() {
        print(evenSemaphore, oddSemaphore);
    }

    private void print(Semaphore current, Semaphore next) {
        for (int i = 1; i <= max; i += 2) {
            try {
                current.acquire();
                System.out.println(Thread.currentThread().getName() + " : " + count++);
                next.release();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
    }
}

// Thread creation (same as before)
ParityPrinter printer = new ParityPrinter(100);
Thread t1 = new Thread(printer::printOdd, "Odd");
Thread t2 = new Thread(printer::printEven, "Even");
t1.start();
t2.start();

Both implementations illustrate how Java’s built‑in synchronization primitives can be used to coordinate two threads to produce ordered output.

Javabackend developmentconcurrencySynchronizationmultithreadingSemaphore
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.