How to print even and odd numbers using threads in java

In this post, we will see how to print even and odd numbers using threads in java.

see also: How to print sequence using 3 threads in java

Problem

You are given two threads. You need to print odd numbers using one thread and even numbers using another thread.You need to print in natural order up to MAX.
For example:
If MAX is 10, you need to print:

1 2 3 4 5 6 7 8 9 10

So 1 3 5 7 9 will be printed by odd thread
2 4 6 8 10 will be printed by even thread.

Further reading

wait, notify and notifyAll method in java with example

wait, notify and notifyAll method in java with example

You might have noticed Object class has three final method called wait, notify and notifyAll. These methods are used for ...
Read More
Print sequence using 3 threads in java

Print sequence using 3 threads in java

In this post, we will see how to print sequence using 3 threads in java.It is similar to printing odd-even ...
Read More

Solution 1

We will use wait and notify to solve how to print even and odd numbers using threads in java.

  • Use a variable called boolean odd. If you want to print odd number, it’s value should be true and vice versa for even number.
  • Create two methods printOdd() and printEven(), one will print odd numbers and other will print even numbers.
  • Create two threads, t2 for odd and t1 for even.
  • t1 will call printEven() method and t2 will call printOdd() method simultaneously.
  • If boolean odd is true in printEven() method, t1 will wait.
  • If boolean odd is false in printOdd() method, t2 will wait.

Print even and odd numbers using threads in java

When you run above program, you will get below output:

Checking odd loop
Odd Thread :1
Checking odd loop
Odd waiting : 2
Checking even loop
Even thread :2
Checking even loop
Even waiting: 3
Notified odd :3
Odd Thread :3
Checking odd loop
Odd waiting : 4
Notified even:4
Even thread :4
Checking even loop
Even waiting: 5
Notified odd :5
Odd Thread :5
Checking odd loop
Odd waiting : 6
Notified even:6
Even thread :6
Checking even loop
Even waiting: 7
Notified odd :7
Odd Thread :7
Checking odd loop
Odd waiting : 8
Notified even:8
Even thread :8
Checking even loop
Even waiting: 9
Notified odd :9
Odd Thread :9
Checking odd loop
Odd waiting : 10
Notified even:10
Even thread :10
Checking even loop
Even waiting: 11
Notified odd :11
Odd Thread :11
Checking odd loop
Odd waiting : 12
Notified even:12
Even thread :12
Checking even loop
Even waiting: 13
Notified odd :13
Odd Thread :13
Checking odd loop
Odd waiting : 14
Notified even:14
Even thread :14
Checking even loop
Even waiting: 15
Notified odd :15
Odd Thread :15
Checking odd loop
Odd waiting : 16
Notified even:16
Even thread :16
Checking even loop
Even waiting: 17
Notified odd :17
Odd Thread :17
Checking odd loop
Odd waiting : 18
Notified even:18
Even thread :18
Checking even loop
Even waiting: 19
Notified odd :19
Odd Thread :19
Notified even:20
Even thread :20

If you observe output, you should be able to understand above program.

Let me try to explain first few lines:
Checking odd loop : t2 Checks for while condition in printOdd() method
Odd Thread :1 : t2 Prints the count ,increment it by one and make odd=false Checking odd loop : Checks for while condition in printOdd() method
Odd waiting : 2: Since odd=false  now, t2 will wait and releases the lock Checking even loop: t1 checks for while condition in printEven() method
Even thread :2 : t1 prints the count,increment it by one and make odd=true Checking even loop: t1 checks for while condition in printEven() method
Even waiting: 3: Since odd=true now, t1 will wait and releases the lock
Notified odd :3 : Since we have called notify() when we were printing Even thread 2, it will notify t2.

All other steps will follow.

Solution 2: Using remainder

You can use concept of remainder here.

  • If number%2==1 then Odd will print the number and increment it else will go in the wait state.
  • If number%2==0 then Even will print the number and increment it else will go in the wait state.

Let’s check with the help of example.

Create a class named OddEvenRunnable and implement Runnable interface.

Create Main class named "PrintOddEvenMain"

When you run above program, you will get below output

Odd 1
Even 2
Odd 3
Even 4
Odd 5
Even 6
Odd 7
Even 8
Odd 9
Even 10

This is all about printing even and odd numbers using threads in java. Please comment if the explanation is not very clear.

You may also like:


import_contacts

You may also like:

Related Posts

  • Get Thread Id in Java
    30 September

    Get Thread Id in Java

    In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but […]

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added to and removed from. We can have […]

  • Delay java program by few seconds
    11 December

    Delay Java program for few secs

    In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. Using Thread.sleep Sleep method causes current thread to pause for […]

  • wait(),notify() and notifyAll() in java
    22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • Custom BlockingQueue in java
    16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. We will use array to store elements in […]

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.