|
69 | 69 | ### **Java**
|
70 | 70 |
|
71 | 71 | ```java
|
| 72 | +class FizzBuzz { |
| 73 | + private int n; |
| 74 | + |
| 75 | + public FizzBuzz(int n) { |
| 76 | + this.n = n; |
| 77 | + } |
| 78 | + |
| 79 | + private Semaphore fSema = new Semaphore(0); |
| 80 | + private Semaphore bSema = new Semaphore(0); |
| 81 | + private Semaphore fbSema = new Semaphore(0); |
| 82 | + private Semaphore nSema = new Semaphore(1); |
| 83 | + |
| 84 | + // printFizz.run() outputs "fizz". |
| 85 | + public void fizz(Runnable printFizz) throws InterruptedException { |
| 86 | + for (int i = 3; i <= n; i = i + 3) { |
| 87 | + if (i % 5 != 0) { |
| 88 | + fSema.acquire(); |
| 89 | + printFizz.run(); |
| 90 | + nSema.release(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // printBuzz.run() outputs "buzz". |
| 96 | + public void buzz(Runnable printBuzz) throws InterruptedException { |
| 97 | + for (int i = 5; i <= n; i = i + 5) { |
| 98 | + if (i % 3 != 0) { |
| 99 | + bSema.acquire(); |
| 100 | + printBuzz.run(); |
| 101 | + nSema.release(); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // printFizzBuzz.run() outputs "fizzbuzz". |
| 107 | + public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { |
| 108 | + for (int i = 15; i <= n; i = i + 15) { |
| 109 | + fbSema.acquire(); |
| 110 | + printFizzBuzz.run(); |
| 111 | + nSema.release(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // printNumber.accept(x) outputs "x", where x is an integer. |
| 116 | + public void number(IntConsumer printNumber) throws InterruptedException { |
| 117 | + for (int i = 1; i <= n; i++) { |
| 118 | + nSema.acquire(); |
| 119 | + if (i % 3 == 0 && i % 5 == 0) { |
| 120 | + fbSema.release(); |
| 121 | + } else if (i % 3 == 0) { |
| 122 | + fSema.release(); |
| 123 | + } else if (i % 5 == 0) { |
| 124 | + bSema.release(); |
| 125 | + } else { |
| 126 | + printNumber.accept(i); |
| 127 | + nSema.release(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
72 | 133 |
|
| 134 | +### **C++** |
| 135 | + |
| 136 | +```cpp |
| 137 | +class FizzBuzz { |
| 138 | +private: |
| 139 | + std::mutex mtx; |
| 140 | + atomic<int> index; |
| 141 | + int n; |
| 142 | + |
| 143 | +public: |
| 144 | + FizzBuzz(int n) { |
| 145 | + this->n = n; |
| 146 | + index = 1; |
| 147 | + } |
| 148 | + |
| 149 | + // printFizz() outputs "fizz". |
| 150 | + void fizz(function<void()> printFizz) { |
| 151 | + while (index <= n) { |
| 152 | + std::lock_guard<std::mutex> lk(mtx); |
| 153 | + if (0 == index % 3 && 0 != index % 5 && index <= n) { |
| 154 | + printFizz(); |
| 155 | + index++; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // printBuzz() outputs "buzz". |
| 161 | + void buzz(function<void()> printBuzz) { |
| 162 | + while (index <= n) { |
| 163 | + std::lock_guard<std::mutex> lk(mtx); |
| 164 | + if (0 == index % 5 && 0 != index % 3 && index <= n) { |
| 165 | + printBuzz(); |
| 166 | + index++; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // printFizzBuzz() outputs "fizzbuzz". |
| 172 | + void fizzbuzz(function<void()> printFizzBuzz) { |
| 173 | + while (index <= n) { |
| 174 | + std::lock_guard<std::mutex> lk(mtx); |
| 175 | + if (0 == index % 15 && index <= n) { |
| 176 | + printFizzBuzz(); |
| 177 | + index++; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // printNumber(x) outputs "x", where x is an integer. |
| 183 | + void number(function<void(int)> printNumber) { |
| 184 | + while (index <= n) { |
| 185 | + std::lock_guard<std::mutex> lk(mtx); |
| 186 | + if (0 != index % 3 && 0 != index % 5 && index <= n) { |
| 187 | + printNumber(index); |
| 188 | + index++; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +}; |
73 | 193 | ```
|
74 | 194 |
|
75 | 195 | ### **...**
|
|
0 commit comments