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
Table of Contents
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:
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:
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()
andprintEven()
, one will print odd numbers and other will print even numbers. - Create two threads,
t2
for odd andt1
for even. t1
will callprintEven()
method andt2
will callprintOdd()
method simultaneously.- If boolean
odd
is true inprintEven()
method,t1
will wait. - If boolean
odd
is false inprintOdd()
method,t2
will wait.
Print even and odd numbers using threads in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package org.arpit.java2blog; public class OddEvenPrintMain { boolean odd; int count = 1; int MAX = 20; public void printOdd() { synchronized (this) { while (count < MAX) { System.out.println("Checking odd loop"); while (!odd) { try { System.out.println("Odd waiting : " + count); wait(); System.out.println("Notified odd :" + count); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Odd Thread :" + count); count++; odd = false; notify(); } } } public void printEven() { try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } synchronized (this) { while (count < MAX) { System.out.println("Checking even loop"); while (odd) { try { System.out.println("Even waiting: " + count); wait(); System.out.println("Notified even:" + count); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Even thread :" + count); count++; odd = true; notify(); } } } public static void main(String[] args) { OddEvenPrintMain oep = new OddEvenPrintMain(); oep.odd = true; Thread t1 = new Thread(new Runnable() { @Override public void run() { oep.printEven(); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { oep.printOdd(); } }); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } |
When you run above program, you will get below output:
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
thenOdd
will print the number and increment it else will go in thewait
state. - If
number%2==0
then Even will print the number and increment it else will go in thewait
state.
Let’s check with the help of example.
Create a class named OddEvenRunnable
and implement Runnable interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
Create a class named <code>OddEvenRunnable</code>. It will implement Runnable interface. public class OddEvenRunnable implements Runnable{ public int PRINT_NUMBERS_UPTO=10; static int number=1; int remainder; static Object lock=new Object(); OddEvenRunnable(int remainder) { this.remainder=remainder; } @Override public void run() { while (number < PRINT_NUMBERS_UPTO) { synchronized (lock) { while (number % 2 != remainder) { // wait for numbers other than remainder try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " " + number); number++; lock.notifyAll(); } } } } |
Create Main class named "PrintOddEvenMain"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class PrintOddEvenMain { public static void main(String[] args) { OddEvenRunnable oddRunnable=new OddEvenRunnable(1); OddEvenRunnable evenRunnable=new OddEvenRunnable(0); Thread t1=new Thread(oddRunnable,"Odd"); Thread t2=new Thread(evenRunnable,"Even"); t1.start(); t2.start(); } } |
When you run above program, you will get below output
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:
- Java multithreading interview questions.
- Java interview questions
- Java interview questions for 5 years experienced
- Java collections interview questions
- Java thread example
The remainder concept is very good and understandable.
Really helpful