Python Lock Threading
\tfrom threading import Lock\n\n\n\tclass ZeroEvenOdd:\n\t\tdef init(self, n):\n\t\t\tself.n = n\n\t\t\tself.zero_lock = Lock()\n\t\t\tself.even_lock = Lock()\n
\tfrom threading import Lock\n\n\n\tclass ZeroEvenOdd:\n\t\tdef __init__(self, n):\n\t\t\tself.n = n\n\t\t\tself.zero_lock = Lock()\n\t\t\tself.even_lock = Lock()\n\t\t\tself.odd_lock = Lock()\n\t\t\tself.even_lock.acquire()\n\t\t\tself.odd_lock.acquire()\n\n\n\t\t# printNumber(x) outputs "x", where x is an integer.\n\t\tdef zero(self, printNumber: \'Callable[[int], None]\') -> None:\n\t\t\tfor i in range(self.n):\n\t\t\t\tself.zero_lock.acquire()\n\t\t\t\tprintNumber(0)\n\t\t\t\tif i % 2 == 0:\n\t\t\t\t\tself.odd_lock.release()\n\t\t\t\telse:\n\t\t\t\t\tself.even_lock.release()\n\n\t\tdef even(self, printNumber: \'Callable[[int], None]\') -> None:\n\t\t\tfor i in range(2, self.n + 1, 2):\n\t\t\t\tself.even_lock.acquire()\n\t\t\t\tprintNumber(i)\n\t\t\t\tself.zero_lock.release()\n\n\t\tdef odd(self, printNumber: \'Callable[[int], None]\') -> None:\n\t\t\tfor i in range(1, self.n + 1 , 2):\n\t\t\t\tself.odd_lock.acquire()\n\t\t\t\tprintNumber(i)\n\t\t\t\tself.zero_lock.release()\n\n
[]