Java Program to print even and odd numbers using 2 threads
Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must be conversant with multithreading.Now, using two separate threads, we must print the even and odd numbers in natural order up to MAX. Therefore, we have two options for resolving the issue: employing wait and notify or the concept of the remainder.
Wait and Notify method
We employ the subsequent methods in order to print the even and odd integers using wait and notify:
1.When we want to print an odd number, we construct the boolean variable odd, whose value needs to be true.
2.To print even and odd numbers, we build two user-defined methods, printEventNumbers() and printOddNumbers().
3.For both even and odd integers, we simultaneously generate two threads, referred to as thread1 and thread2.
4.Both the printEvenNumbers() method and the printOddNumbers() method will be called simultaneously by threads 1 and 2.
5.The thread 1 will wait until printEvenNumbers' boolean odd value is true ().
6.The thread 2 will wait till the boolean odd value in printOddNumbers is false ().
Program for even and odd using Threads
EvenOdd
import java . util . Scanner ;
public class EvenOdd {
boolean odd ;
int c = 1 ;
int MAX ;
public void Odd ( ) {
synchronized ( this ) {
while ( c < MAX ) {
System . out . println ( "odd numbers call block " ) ;
while ( ! odd ) {
try {
System . out . println ( " Odd waiting : " + c ) ;
wait ( ) ;
System . out . println ( " odd : " + c ) ;
} catch ( InterruptedException e1 ) {
e1 . printStackTrace ( ) ;
}
}
System . out . println ( " Odd Thread : " + c ) ;
c ++ ;
odd = false ;
notify ( ) ;
}
}
}
public void Even ( ) {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
synchronized ( this ) {
while ( c < MAX ) {
System . out . println ( " even number call " ) ;
while ( odd ) {
try {
System . out . println ( " Even waiting : " + c ) ;
wait ( ) ;
System . out . println ( " even : " + c ) ;
} catch ( InterruptedException e2 ) {
e2 . printStackTrace ( ) ;
}
}
System . out . println ( " Even Thread : " + c ) ;
c ++ ;
odd = false ;
notify ( ) ;
}
}
}
public static void main ( String [ ] args ) {
final EvenOdd obj = new EvenOdd ( ) ;
obj . odd = true ;
Scanner sc = new Scanner ( System . in ) ;
System . out . println( " Enter MAX value : " ) ;
obj . MAX = sc . nextInt ( ) ;
sc . close ( ) ;
Thread t1 = new Thread ( new Runnable ( ) {
public void run ( ) {
obj . Even ( ) ;
}
} ) ;
Thread t2 = new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
obj . Odd ( ) ;
}
} ) ;
t1 . start ( ) ;
t2 . start ( ) ;
try {
t1 . join ( ) ;
t2 . join ( ) ;
} catch (InterruptedException e) {
e . printStackTrace ( );
}
}
}
Output

Method 2
// importing requied packages
public class EvenOdd {
int counter = 1 ;
// staticvariable declaration
static int N ;
public void printOddNumber ( )
{
synchronized ( this )
{
while ( counter < N ) {
while ( counter % 2 == 0 ) {
try {
wait ( ) ;
}
catch (
InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
System . out . print ( counter + " " ) ;
counter ++ ;
notify ( ) ;
}
}
}
public void printEvenNumber ( )
{
synchronized ( this )
{
while ( counter < N ) {
while ( counter % 2 == 1 ) {
// using exception handling keywords
try {
wait ( ) ;
}
catch (
InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
System . out . print ( counter + " " ) ;
counter ++ ;
notify ( ) ;
}
}
}
public static void main ( String [ ] args )
{
N = 10 ;
EvenOdd mt = new EvenOdd ( ) ;
Thread t1 = new Thread ( new Runnable ( ) {
public void run ( )
{
mt . printEvenNumber ( ) ;
}
} ) ;
Thread t2 = new Thread ( new Runnable ( ) {
public void run()
{
mt . printOddNumber ( ) ;
}
} ) ;
t1 . start ( ) ;
t2 . start ( ) ;
}
}
Output
