Multithreading in Java: Even and Odd Numbers Program Explained

Multithreading in Java Even and Odd Numbers Program Explained banner with thread visualization and code example

Multithreading is one of the most useful concepts in Java, especially when you want to execute multiple tasks simultaneously. A classic beginner-friendly example is printing even and odd numbers using two separate threads.

In this article, you’ll learn how to use multithreading in Java to print even and odd numbers efficiently using two threads.


What is Multithreading?

Multithreading allows a program to run multiple threads at the same time. Each thread performs a separate task, improving performance and responsiveness.

In this example:

  • One thread prints even numbers
  • Another thread prints odd numbers

Why Use Threads for Even-Odd Numbers?

  • Demonstrates parallel execution
  • Helps understand thread behavior
  • Improves logical thinking
  • Common exam question

Program: Even-Odd Numbers using Threads

Let’s create two threads:

  • EvenThread → prints even numbers
  • OddThread → prints odd numbers
Even-Odd Threads Example
class EvenThread extends Thread {
    public void run() {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                System.out.println("Even: " + i);
            }
            try {
                Thread.sleep(300);
            } catch (Exception e) {}
        }
    }
}

class OddThread extends Thread {
    public void run() {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 != 0) {
                System.out.println("Odd: " + i);
            }
            try {
                Thread.sleep(300);
            } catch (Exception e) {}
        }
    }
}

public class TestEvenOdd {
    public static void main(String[] args) {
        EvenThread t1 = new EvenThread();
        OddThread t2 = new OddThread();

        t1.start();
        t2.start();
    }
}
  

Sample Output

Output
Odd: 1
Even: 2
Odd: 3
Even: 4
Odd: 5
Even: 6
...
  

Note: Output may vary because threads run independently.


How This Program Works

  • Two classes extend Thread
  • Each overrides the run() method
  • EvenThread prints even numbers
  • OddThread prints odd numbers
  • start() runs both threads simultaneously

Using Runnable Interface (Alternative Method)

You can also implement threads using the Runnable interface.

Runnable Example
class EvenRunnable implements Runnable {
    public void run() {
        for (int i = 2; i <= 10; i += 2) {
            System.out.println("Even: " + i);
        }
    }
}

class OddRunnable implements Runnable {
    public void run() {
        for (int i = 1; i <= 10; i += 2) {
            System.out.println("Odd: " + i);
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Thread t1 = new Thread(new EvenRunnable());
        Thread t2 = new Thread(new OddRunnable());

        t1.start();
        t2.start();
    }
}
  

Key Concepts Used

  • Multithreading
  • Thread class
  • Runnable interface
  • Concurrent execution

Real-World Applications

  • Parallel processing systems
  • Real-time applications
  • Background tasks
  • Gaming and UI responsiveness

Common Mistakes

  • Calling run() instead of start()
  • Ignoring thread synchronization
  • Not handling exceptions

Best Practices

  • Use Runnable for flexibility
  • Keep threads lightweight
  • Handle exceptions properly

FAQ

What is multithreading?

Running multiple threads at the same time in a program.

Why use threads for even-odd?

To demonstrate parallel execution.

What is start() method?

It creates and starts a new thread.


Conclusion

This even-odd numbers example is a simple yet powerful way to understand multithreading in Java. It shows how multiple threads can work independently to perform tasks simultaneously.

Try modifying this program to deepen your understanding of threads.


📚 Read More

Comments