You have a function printNumber that can be called with an integer parameter and prints it to the console.
For example, calling printNumber(7) prints 7 to the console.
You are given an instance of the class ZeroEvenOdd that has three functions: zero, even, and odd. The same instance of ZeroEvenOdd will be passed to three different threads:
Thread A: calls zero() that should only output 0’s.
Thread B: calls even() that should only output even numbers.
Thread C: calls odd() that should only output odd numbers.
Modify the given class to output the series "010203040506..." where the length of the series must be 2n.
Implement the ZeroEvenOdd class:
ZeroEvenOdd(int n) Initializes the object with the number n that represents the numbers that should be printed.
void zero(printNumber) Calls printNumber to output one zero.
void even(printNumber) Calls printNumber to output one even number.
void odd(printNumber) Calls printNumber to output one odd number.
Input: n = 2
Output: "0102"
Explanation: There are three threads being fired asynchronously.
One of them calls zero(), the other calls even(), and the last one calls odd().
"0102" is the correct output.
We use three semaphores to strictly control the order of execution between the three threads. The zero thread starts, prints 0, and then signals either the odd or even thread. After printing, the odd/even thread signals the zero thread to continue.
import threading
classZeroEvenOddSemaphore:
def__init__(self, n: int):
self.n = n
self.zeroSem = threading.Semaphore(1)
self.oddSem = threading.Semaphore(0)
self.evenSem = threading.Semaphore(0)
defzero(self, printNumber):
for i in range(self.n):
self.zeroSem.acquire()
printNumber(0)
if i %2==0:
self.oddSem.release()
else:
self.evenSem.release()
defeven(self, printNumber):
for i in range(2, self.n +1, 2):
self.evenSem.acquire()
printNumber(i)
self.zeroSem.release()
defodd(self, printNumber):
for i in range(1, self.n +1, 2):
self.oddSem.acquire()
printNumber(i)
self.zeroSem.release()
We use a shared state variable and synchronized methods to coordinate the three threads. Each thread waits for its turn using wait() and notifies the others using notifyAll() after printing.
import java.util.function.IntConsumer;
classZeroEvenOddSync {
privateint n;
privateint sem = 0;
publicZeroEvenOddSync(int n) {
this.n= n;
}
publicsynchronizedvoidzero(IntConsumer printNumber) throws InterruptedException {
for (int i = 0; i < n; ++i) {
while (sem != 0)
wait();
printNumber.accept(0);
sem = i % 2 == 0 ? 1 : 2;
notifyAll();
}
}
publicsynchronizedvoideven(IntConsumer printNumber) throws InterruptedException {
for (int i = 2; i <= n; i += 2) {
while (sem != 2)
wait();
printNumber.accept(i);
sem = 0;
notifyAll();
}
}
publicsynchronizedvoidodd(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i += 2) {
while (sem != 1)
wait();
printNumber.accept(i);
sem = 0;
notifyAll();
}
}
}
classZeroEvenOddSync(privateval n: Int) {
privatevar sem = 0@Synchronizedfunzero(printNumber: (Int) -> Unit) {
for (i in0 until n) {
while (sem !=0) {
(thisas java.lang.Object).wait()
}
printNumber(0)
sem = if (i % 2==0) 1else2 (thisas java.lang.Object).notifyAll()
}
}
@Synchronizedfuneven(printNumber: (Int) -> Unit) {
for (i in2..n step 2) {
while (sem !=2) {
(thisas java.lang.Object).wait()
}
printNumber(i)
sem = 0 (thisas java.lang.Object).notifyAll()
}
}
@Synchronizedfunodd(printNumber: (Int) -> Unit) {
for (i in1..n step 2) {
while (sem !=1) {
(thisas java.lang.Object).wait()
}
printNumber(i)
sem = 0 (thisas java.lang.Object).notifyAll()
}
}
}
import threading
classZeroEvenOddSync:
def__init__(self, n: int):
self.n = n
self.sem =0 self.lock = threading.Condition()
defzero(self, printNumber):
for i in range(self.n):
with self.lock:
while self.sem !=0:
self.lock.wait()
printNumber(0)
self.sem =1if i %2==0else2 self.lock.notify_all()
defeven(self, printNumber):
for i in range(2, self.n +1, 2):
with self.lock:
while self.sem !=2:
self.lock.wait()
printNumber(i)
self.sem =0 self.lock.notify_all()
defodd(self, printNumber):
for i in range(1, self.n +1, 2):
with self.lock:
while self.sem !=1:
self.lock.wait()
printNumber(i)
self.sem =0 self.lock.notify_all()