Multiple threads odd-even refactor (#5617)
* encoding * Converting synchronous and asynchronous API to observables * Adding different ways of converting sync/async APIs to observalbles. * Replace anonymous class with lambda * update based on comment from Grzegorz Piwowarek * Refactor even-odd semaphore demo * Remove unrelated files
This commit is contained in:
parent
8232be4841
commit
03f530126f
|
@ -2,55 +2,47 @@ package com.baeldung.concurrent.evenandodd;
|
|||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class SemaphoreDemo {
|
||||
public class PrintEvenOddSemaphore {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SharedPrinter sp = new SharedPrinter();
|
||||
Thread odd = new Thread(new Odd(sp, 10));
|
||||
odd.setName("Odd");
|
||||
Thread even = new Thread(new Even(sp, 10));
|
||||
even.setName("Even");
|
||||
Thread odd = new Thread(new Odd(sp, 10), "Odd");
|
||||
Thread even = new Thread(new Even(sp, 10), "Even");
|
||||
|
||||
odd.start();
|
||||
even.start();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SharedPrinter {
|
||||
|
||||
Semaphore semEven = new Semaphore(0);
|
||||
Semaphore semOdd = new Semaphore(1);
|
||||
private final Semaphore semEven = new Semaphore(0);
|
||||
private final Semaphore semOdd = new Semaphore(1);
|
||||
|
||||
public void printEvenNum(int num) {
|
||||
void printEvenNum(int num) {
|
||||
try {
|
||||
semEven.acquire();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
System.out.println(Thread.currentThread()
|
||||
.getName() + ":"+num);
|
||||
System.out.println(Thread.currentThread().getName() + ":"+num);
|
||||
semOdd.release();
|
||||
}
|
||||
|
||||
public void printOddNum(int num) {
|
||||
void printOddNum(int num) {
|
||||
try {
|
||||
semOdd.acquire();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
System.out.println(Thread.currentThread()
|
||||
.getName() + ":"+ num);
|
||||
System.out.println(Thread.currentThread().getName() + ":"+ num);
|
||||
semEven.release();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Even implements Runnable {
|
||||
SharedPrinter sp;
|
||||
int max;
|
||||
private final SharedPrinter sp;
|
||||
private final int max;
|
||||
|
||||
Even(SharedPrinter sp, int max) {
|
||||
this.sp = sp;
|
||||
|
@ -66,8 +58,8 @@ class Even implements Runnable {
|
|||
}
|
||||
|
||||
class Odd implements Runnable {
|
||||
SharedPrinter sp;
|
||||
int max;
|
||||
private SharedPrinter sp;
|
||||
private int max;
|
||||
|
||||
Odd(SharedPrinter sp, int max) {
|
||||
this.sp = sp;
|
|
@ -1,22 +1,20 @@
|
|||
package com.baeldung.concurrent.evenandodd;
|
||||
|
||||
public class PrintEvenOdd {
|
||||
public class PrintEvenOddWaitNotify {
|
||||
|
||||
public static void main(String... args) {
|
||||
Printer print = new Printer();
|
||||
Thread t1 = new Thread(new TaskEvenOdd(print, 10, false));
|
||||
t1.setName("Odd");
|
||||
Thread t2 = new Thread(new TaskEvenOdd(print, 10, true));
|
||||
t2.setName("Even");
|
||||
Thread t1 = new Thread(new TaskEvenOdd(print, 10, false), "Odd");
|
||||
Thread t2 = new Thread(new TaskEvenOdd(print, 10, true), "Even");
|
||||
t1.start();
|
||||
t2.start();
|
||||
}
|
||||
}
|
||||
|
||||
class TaskEvenOdd implements Runnable {
|
||||
private int max;
|
||||
private Printer print;
|
||||
private boolean isEvenNumber;
|
||||
private final int max;
|
||||
private final Printer print;
|
||||
private final boolean isEvenNumber;
|
||||
|
||||
TaskEvenOdd(Printer print, int max, boolean isEvenNumber) {
|
||||
this.print = print;
|
||||
|
@ -26,7 +24,7 @@ class TaskEvenOdd implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
int number = isEvenNumber == true ? 2 : 1;
|
||||
int number = isEvenNumber ? 2 : 1;
|
||||
while (number <= max) {
|
||||
if (isEvenNumber) {
|
||||
print.printEven(number);
|
||||
|
@ -39,32 +37,30 @@ class TaskEvenOdd implements Runnable {
|
|||
}
|
||||
|
||||
class Printer {
|
||||
boolean isOdd = false;
|
||||
private volatile boolean isOdd;
|
||||
|
||||
synchronized void printEven(int number) {
|
||||
while (isOdd == false) {
|
||||
while (!isOdd) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
System.out.println(Thread.currentThread()
|
||||
.getName() + ":" + number);
|
||||
System.out.println(Thread.currentThread().getName() + ":" + number);
|
||||
isOdd = false;
|
||||
notify();
|
||||
}
|
||||
|
||||
synchronized void printOdd(int number) {
|
||||
while (isOdd == true) {
|
||||
while (isOdd) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
System.out.println(Thread.currentThread()
|
||||
.getName() + ":" + number);
|
||||
System.out.println(Thread.currentThread().getName() + ":" + number);
|
||||
isOdd = true;
|
||||
notify();
|
||||
}
|
Loading…
Reference in New Issue