Java Multithreading Interview Coding Question: Print even odd numbers using two synchronize thread in java or java 8 or in stream API!
Hello Readers / Coding Geeks,
Today, we are solving one interview coding question in JAVA ask in big product base MNC to a SSE (Software Engineer).
Question: Print even odd numbers using java multithreading two threads in synchronized manner.
![]() |
Java Multithreading Interview Coding Question: Print even odd numbers using two synchronize thread in java or java 8 or in stream API! |
Solution 1: (In JAVA 7):
public class PrintEvenOddUsingTwoThreadsMultithreading implements Runnable {
static int count = 1;
Object object;
public PrintEvenOddUsingTwoThreadsMultithreading(Object object) {
this.object = object;
}
@Override
public void run() {
while (count < 10) {
if (count % 2 == 0 && Thread.currentThread().getName().equals("even")) {
synchronized (object) {
System.out.println("Thread Name: "
+ Thread.currentThread().getName()
+ " value : "
+ count);
count++;
try {
object.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
if (count % 2 != 0 && Thread.currentThread().getName().equals("odd")) {
synchronized (object) {
System.out.println("Thread Name: "
+ Thread.currentThread().getName()
+ " value : "
+ count);
count++;
object.notify();
}
}
}
}
public static void main(String[] args) {
Object obj = new Object();
Runnable t1 = new PrintEvenOddUsingTwoThreadsMultithreading(obj);
Runnable t2 = new PrintEvenOddUsingTwoThreadsMultithreading(obj);
new Thread(t1, "even").start();
new Thread(t2, "odd").start();
}
}
--------------------------
Output:
Thread Name: odd value : 1
Thread Name: even value : 2
Thread Name: odd value : 3
Thread Name: even value : 4
Thread Name: odd value : 5
Thread Name: even value : 6
Thread Name: odd value : 7
Thread Name: even value : 8
Thread Name: odd value : 9
Solution 2: Using Java 8 Executor service:
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class PrintEvenOddUsingTwoThreadsByES {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
IntStream.rangeClosed(1, 10)
.forEach(num -> {
CompletableFuture<Integer> oddCompletableFuture = CompletableFuture.completedFuture(num)
.thenApplyAsync(x -> {
if (x % 2 != 0) {
System.out.println("Thread Name: "
+ Thread.currentThread().getName()
+ " Value: "
+ x);
}
return num;
}, executorService);
oddCompletableFuture.join();
});
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class PrintEvenOddUsingTwoThreadsByES {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
IntStream.rangeClosed(1, 10)
.forEach(num -> {
CompletableFuture<Integer> oddCompletableFuture = CompletableFuture.completedFuture(num)
.thenApplyAsync(x -> {
if (x % 2 != 0) {
System.out.println("Thread Name: "
+ Thread.currentThread().getName()
+ " Value: "
+ x);
}
return num;
}, executorService);
oddCompletableFuture.join();
});
IntStream.rangeClosed(1, 10)
.forEach(num -> {
CompletableFuture<Integer> evenCompletableFuture = CompletableFuture.completedFuture(num)
.thenApplyAsync(x -> {
if (x % 2 == 0) {
System.out.println("Thread Name: "
+ Thread.currentThread().getName()
+ " Value: "
+ x);
}
return num;
}, executorService);
evenCompletableFuture.join();
});
executorService.shutdown();
}
}
---------------------------
Output:
Thread Name: pool-1-thread-1 Value: 1
Thread Name: pool-1-thread-1 Value: 3
Thread Name: pool-1-thread-1 Value: 5
Thread Name: pool-1-thread-1 Value: 7
Thread Name: pool-1-thread-1 Value: 9
Thread Name: pool-1-thread-2 Value: 2
Thread Name: pool-1-thread-2 Value: 4
Thread Name: pool-1-thread-2 Value: 6
Thread Name: pool-1-thread-2 Value: 8
Thread Name: pool-1-thread-2 Value: 10
Solution 3: Using Java 8 Stream Completable Feature:
import java.util.concurrent.CompletableFuture;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
public class EvenAndOddPrinter {
private static Object object = new Object();
private static IntPredicate evenCondition = e -> e % 2 == 0;
private static IntPredicate oddCondition = e -> e % 2 != 0;
public static void main(String[] args) throws InterruptedException {
CompletableFuture.runAsync(() -> EvenAndOddPrinter.printResults(oddCondition));
CompletableFuture.runAsync(() -> EvenAndOddPrinter.printResults(evenCondition));
Thread.sleep(1000);
}
public static void printResults(IntPredicate condition) {
IntStream.rangeClosed(1, 10)
.filter(condition)
.forEach(EvenAndOddPrinter::execute);
}
public static void execute(int i) {
synchronized (object) {
try {
System.out.println("Thread Name "
+ Thread.currentThread().getName()
+ " : " + i);
object.notify();
object.wait();
} catch (InterruptedException ex) {
//error log
}
}
}
}
Output:
Thread Name ForkJoinPool.commonPool-worker-3 : 1
Thread Name ForkJoinPool.commonPool-worker-5 : 2
Thread Name ForkJoinPool.commonPool-worker-3 : 3
Thread Name ForkJoinPool.commonPool-worker-5 : 4
Thread Name ForkJoinPool.commonPool-worker-3 : 5
Thread Name ForkJoinPool.commonPool-worker-5 : 6
Thread Name ForkJoinPool.commonPool-worker-3 : 7
Thread Name ForkJoinPool.commonPool-worker-5 : 8
Thread Name ForkJoinPool.commonPool-worker-3 : 9
Thread Name ForkJoinPool.commonPool-worker-5 : 10
0 Comments