File tree Expand file tree Collapse file tree 3 files changed +121
-0
lines changed
Expand file tree Collapse file tree 3 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .javatechie ;
2+
3+ import java .util .concurrent .CompletableFuture ;
4+ import java .util .function .IntPredicate ;
5+ import java .util .stream .IntStream ;
6+
7+ public class EvenAndOddPrinter {
8+
9+ private static Object object = new Object ();
10+
11+ private static IntPredicate evenCondition = e -> e % 2 == 0 ;
12+ private static IntPredicate oddCondition = e -> e % 2 != 0 ;
13+
14+
15+ public static void main (String [] args ) throws InterruptedException {
16+ CompletableFuture .runAsync (() -> EvenAndOddPrinter .printResults (oddCondition ));
17+ CompletableFuture .runAsync (() -> EvenAndOddPrinter .printResults (evenCondition ));
18+ Thread .sleep (1000 );
19+ }
20+
21+ public static void printResults (IntPredicate condition ) {
22+ IntStream .rangeClosed (1 , 10 )
23+ .filter (condition )
24+ .forEach (EvenAndOddPrinter ::execute );
25+ }
26+
27+
28+ public static void execute (int i ) {
29+ synchronized (object ) {
30+ try {
31+ System .out .println ("Thread Name " + Thread .currentThread ().getName () + " : " + i );
32+ object .notify ();
33+ object .wait ();
34+ } catch (InterruptedException ex ) {
35+ //error log
36+ }
37+ }
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .javatechie ;
2+
3+ public class EvenAndOddPrinterBy2Threads implements Runnable {
4+
5+ static int count = 1 ;
6+ Object object ;
7+
8+ public EvenAndOddPrinterBy2Threads (Object object ) {
9+ this .object = object ;
10+ }
11+
12+ @ Override
13+ public void run () {
14+
15+ while (count <= 100 ) {
16+ if (count % 2 == 0 && Thread .currentThread ().getName ().equals ("even" )) {
17+ synchronized (object ) {
18+ System .out .println ("Thread Name : " + Thread .currentThread ().getName () + " value :" + count );
19+ count ++;
20+ try {
21+ object .wait ();
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+ }
26+ }
27+ if (count % 2 != 0 && Thread .currentThread ().getName ().equals ("odd" )) {
28+ synchronized (object ) {
29+ System .out .println ("Thread Name : " + Thread .currentThread ().getName () + " value :" + count );
30+ count ++;
31+ object .notify ();
32+ }
33+ }
34+
35+ }
36+
37+ }
38+
39+ public static void main (String [] args ) {
40+ Object lock =new Object ();
41+ Runnable r1 =new EvenAndOddPrinterBy2Threads (lock );
42+ Runnable r2 =new EvenAndOddPrinterBy2Threads (lock );
43+ new Thread (r1 , "even" ).start ();
44+ new Thread (r2 , "odd" ).start ();
45+ }
46+ }
Original file line number Diff line number Diff line change 1+ package com .javatechie ;
2+
3+ import java .util .concurrent .CompletableFuture ;
4+ import java .util .concurrent .ExecutorService ;
5+ import java .util .concurrent .Executors ;
6+ import java .util .stream .IntStream ;
7+
8+ public class EvenAndOddPrinterByES {
9+
10+ public static void main (String [] args ) {
11+
12+ ExecutorService executorService = Executors .newFixedThreadPool (2 );
13+
14+ IntStream .rangeClosed (1 , 10 )
15+ .forEach (num -> {
16+ CompletableFuture <Integer > oddCompletableFuture = CompletableFuture .completedFuture (num )
17+ .thenApplyAsync (x -> {
18+ if (x % 2 != 0 ) {
19+ System .out .println ("Thread Name " + Thread .currentThread ().getName () + " : " + x );
20+ }
21+ return num ;
22+ }, executorService );
23+ oddCompletableFuture .join ();
24+
25+ CompletableFuture <Integer > evenCompletableFuture = CompletableFuture .completedFuture (num )
26+ .thenApplyAsync (x -> {
27+ if (x % 2 == 0 ) {
28+ System .out .println ("Thread Name " + Thread .currentThread ().getName () + " : " + x );
29+ }
30+ return num ;
31+ }, executorService );
32+ evenCompletableFuture .join ();
33+ });
34+ executorService .shutdown ();
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments