| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Concurrency.FizzBuzzMultithreaded; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class FizzBuzzMultithreadedMonitorLock : IFizzBuzzMultithreaded |
| | | 16 | | { |
| | 5 | 17 | | private readonly object _lock = new(); |
| | | 18 | | private readonly int _n; |
| | 5 | 19 | | private int _number = 1; |
| | | 20 | | |
| | 5 | 21 | | public FizzBuzzMultithreadedMonitorLock(int n) |
| | 5 | 22 | | { |
| | 5 | 23 | | _n = n; |
| | 5 | 24 | | } |
| | | 25 | | |
| | | 26 | | public void Fizz(Action printFizz) |
| | 5 | 27 | | { |
| | 5 | 28 | | lock (_lock) |
| | 5 | 29 | | { |
| | 11 | 30 | | while (_number <= _n) |
| | 9 | 31 | | { |
| | 14 | 32 | | while (_number <= _n && (_number % 3 != 0 || _number % 5 == 0)) |
| | 8 | 33 | | { |
| | 8 | 34 | | Monitor.Wait(_lock); |
| | | 35 | | |
| | 8 | 36 | | if (_number > _n) |
| | 3 | 37 | | { |
| | 3 | 38 | | return; |
| | | 39 | | } |
| | 5 | 40 | | } |
| | | 41 | | |
| | 6 | 42 | | printFizz.Invoke(); |
| | | 43 | | |
| | 6 | 44 | | IncrementNumber(); |
| | 6 | 45 | | } |
| | 2 | 46 | | } |
| | 5 | 47 | | } |
| | | 48 | | |
| | | 49 | | public void Buzz(Action printBuzz) |
| | 5 | 50 | | { |
| | 5 | 51 | | lock (_lock) |
| | 5 | 52 | | { |
| | 8 | 53 | | while (_number <= _n) |
| | 4 | 54 | | { |
| | 9 | 55 | | while (_number % 3 == 0 || _number % 5 != 0) |
| | 6 | 56 | | { |
| | 6 | 57 | | Monitor.Wait(_lock); |
| | | 58 | | |
| | 6 | 59 | | if (_number > _n) |
| | 1 | 60 | | { |
| | 1 | 61 | | return; |
| | | 62 | | } |
| | 5 | 63 | | } |
| | | 64 | | |
| | 3 | 65 | | printBuzz.Invoke(); |
| | | 66 | | |
| | 3 | 67 | | IncrementNumber(); |
| | 3 | 68 | | } |
| | 4 | 69 | | } |
| | 5 | 70 | | } |
| | | 71 | | |
| | | 72 | | public void Fizzbuzz(Action printFizzBuzz) |
| | 5 | 73 | | { |
| | 5 | 74 | | lock (_lock) |
| | 5 | 75 | | { |
| | 6 | 76 | | while (_number <= _n) |
| | 2 | 77 | | { |
| | 2 | 78 | | while (_number % 3 != 0 || _number % 5 != 0) |
| | 1 | 79 | | { |
| | 1 | 80 | | Monitor.Wait(_lock); |
| | | 81 | | |
| | 1 | 82 | | if (_number > _n) |
| | 1 | 83 | | { |
| | 1 | 84 | | return; |
| | | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | printFizzBuzz.Invoke(); |
| | | 89 | | |
| | 1 | 90 | | IncrementNumber(); |
| | 1 | 91 | | } |
| | 4 | 92 | | } |
| | 5 | 93 | | } |
| | | 94 | | |
| | | 95 | | public void Number(Action<int> printNumber) |
| | 5 | 96 | | { |
| | 5 | 97 | | lock (_lock) |
| | 5 | 98 | | { |
| | 21 | 99 | | while (_number <= _n) |
| | 19 | 100 | | { |
| | 26 | 101 | | while (_number % 3 == 0 || _number % 5 == 0) |
| | 10 | 102 | | { |
| | 10 | 103 | | Monitor.Wait(_lock); |
| | | 104 | | |
| | 10 | 105 | | if (_number > _n) |
| | 3 | 106 | | { |
| | 3 | 107 | | return; |
| | | 108 | | } |
| | 7 | 109 | | } |
| | | 110 | | |
| | 16 | 111 | | printNumber.Invoke(_number); |
| | | 112 | | |
| | 16 | 113 | | IncrementNumber(); |
| | 16 | 114 | | } |
| | 2 | 115 | | } |
| | 5 | 116 | | } |
| | | 117 | | |
| | | 118 | | private void IncrementNumber() |
| | 26 | 119 | | { |
| | 26 | 120 | | _number++; |
| | | 121 | | |
| | 26 | 122 | | Monitor.PulseAll(_lock); |
| | 26 | 123 | | } |
| | | 124 | | } |