Identifying Even & Odd numbers by the help of different Thread

Durjoy Acharjya
2 min readAug 17, 2023

--

To Solve this problem I’ll be use thread synchronization with inter thread communication

Here I’ll be go step by step first I try with java 7 then I implement it via java 8

Even and Odd Printed by different thread

first I need to implement our class from Runnable interface (Here you can use callable or by extending thread class)

//java 7 approch
public class IdentifyEvenAndOddNumbersByThread implements Runnable{
//i want to print from the 1 to the limit
//i'll specify it in the while loop
static int count=1;
public Object object;

public IdentifyEvenAndOddNumbersByThread(Object object) {
this.object = object;
}
@Override
public void run() {
//upto 100
while (count<=100){
if (count%2==0 && Thread.currentThread().getName().equals("even"))
{
//we sync one after another by the help of synchronized () block
synchronized (object){
System.out.println("Thread Name: "+Thread.currentThread().getName()+" Value: "+count);
//once i print it i need to immediately increase the count variable
count++;
//i use wait and notify for the inter thread communication
try {
object.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
if (count%2!=0 && Thread.currentThread().getName().equals("odd")){
//this thread will print the odd number and notify once he is done
//then again he will get a chance to execute
synchronized (object){
System.out.println("Thread Name: "+Thread.currentThread().getName()+" Value: "+count);
//once i print it i need to immediately increase the count variable
count++;
object.notify();
}
}
}
}

public static void main(String[] args) {
//we need to declare an object here because we want to perform locking in the same object
Object lock=new Object();
Runnable object1=new IdentifyEvenAndOddNumbersByThread(lock);//now the parameter is crying why it is crying cz expecting the object
Runnable object2=new IdentifyEvenAndOddNumbersByThread(lock);//now the parameter is crying why it is crying cz expecting the object

//we can not execute runnable directly we need to pass runnable instance in the Thread class constructor
//here i need to define the thread name i want to this thread as a even thread
new Thread(object1,"even").start();///immediately start
//here i need to define the thread name i want to this thread as a odd thread
new Thread(object2,"odd").start();///immediately start
}
}
Output

Now Let’s implement using java 8

import java.util.concurrent.*;
import java.util.stream.IntStream;

public class IdentifyEvenAndOddNumbersByThread {

public static void main(String[] args) {
ExecutorService service= Executors.newFixedThreadPool(2);
IntStream.range(1,101)
.forEach(
number->{
CompletableFuture<Integer> evenFuture = CompletableFuture
.completedFuture(number)
.thenApplyAsync(e->{
if (e%2==0)
System.out.println("Thread Name: "+Thread.currentThread().getName()+" : "+number);
return number;
},service);
evenFuture.join();
CompletableFuture<Integer> oddFuture = CompletableFuture
.completedFuture(number)
.thenApplyAsync(e->{
if (e%2!=0)
System.out.println("Thread Name: "+Thread.currentThread().getName()+" : "+number);
return number;
},service);
oddFuture.join();
}
);
}
}
Output

Thank you

--

--

Durjoy Acharjya
Durjoy Acharjya

Written by Durjoy Acharjya

🚀 Passionate Programmer | 🌐 Angular | ⚡ .NET Core | ☁️ Spring Boot | 🔧 AWS | 🔒 DevOps | 🌱 MLOps | 🛡️ DevSecOps | 📊 T-SQL | 📡 Kafka 📱 Swift | 🍏 iOS

No responses yet