bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1116. Print Zero Even Odd

1116. Print Zero Even Odd

Leetcode Solutions

Synchronized Methods with Wait/Notify

  1. Initialize the shared variables zeroPrinted and currentNumber.
  2. In the zero method, use a synchronized block to wait until zeroPrinted is false, print 0, set zeroPrinted to true, and notify all waiting threads.
  3. In the even method, use a synchronized block to wait until currentNumber is even and zeroPrinted is true, print the current number, increment currentNumber, set zeroPrinted to false, and notify all waiting threads.
  4. In the odd method, use a synchronized block to wait until currentNumber is odd and zeroPrinted is true, print the current number, increment currentNumber, set zeroPrinted to false, and notify all waiting threads.
  5. Repeat steps 2-4 until currentNumber exceeds n.
UML Thumbnail

Atomic Variables with Busy Waiting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...