| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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 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) |
| 10 | 31 | | { |
| 18 | 32 | | while (_number <= _n && (_number % 3 != 0 || _number % 5 == 0)) |
| 12 | 33 | | { |
| 12 | 34 | | Monitor.Wait(_lock); |
| | 35 | |
|
| 12 | 36 | | if (_number > _n) |
| 4 | 37 | | { |
| 4 | 38 | | return; |
| | 39 | | } |
| 8 | 40 | | } |
| | 41 | |
|
| 6 | 42 | | printFizz.Invoke(); |
| | 43 | |
|
| 6 | 44 | | IncrementNumber(); |
| 6 | 45 | | } |
| 1 | 46 | | } |
| 5 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Buzz(Action printBuzz) |
| 5 | 50 | | { |
| 5 | 51 | | lock (_lock) |
| 5 | 52 | | { |
| 8 | 53 | | while (_number <= _n) |
| 7 | 54 | | { |
| 16 | 55 | | while (_number % 3 == 0 || _number % 5 != 0) |
| 13 | 56 | | { |
| 13 | 57 | | Monitor.Wait(_lock); |
| | 58 | |
|
| 13 | 59 | | if (_number > _n) |
| 4 | 60 | | { |
| 4 | 61 | | return; |
| | 62 | | } |
| 9 | 63 | | } |
| | 64 | |
|
| 3 | 65 | | printBuzz.Invoke(); |
| | 66 | |
|
| 3 | 67 | | IncrementNumber(); |
| 3 | 68 | | } |
| 1 | 69 | | } |
| 5 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Fizzbuzz(Action printFizzBuzz) |
| 5 | 73 | | { |
| 5 | 74 | | lock (_lock) |
| 5 | 75 | | { |
| 6 | 76 | | while (_number <= _n) |
| 4 | 77 | | { |
| 12 | 78 | | while (_number % 3 != 0 || _number % 5 != 0) |
| 11 | 79 | | { |
| 11 | 80 | | Monitor.Wait(_lock); |
| | 81 | |
|
| 11 | 82 | | if (_number > _n) |
| 3 | 83 | | { |
| 3 | 84 | | return; |
| | 85 | | } |
| 8 | 86 | | } |
| | 87 | |
|
| 1 | 88 | | printFizzBuzz.Invoke(); |
| | 89 | |
|
| 1 | 90 | | IncrementNumber(); |
| 1 | 91 | | } |
| 2 | 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 | | { |
| 24 | 101 | | while (_number % 3 == 0 || _number % 5 == 0) |
| 8 | 102 | | { |
| 8 | 103 | | Monitor.Wait(_lock); |
| | 104 | |
|
| 8 | 105 | | if (_number > _n) |
| 3 | 106 | | { |
| 3 | 107 | | return; |
| | 108 | | } |
| 5 | 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 | | } |