// printFizz.run() outputs "fizz". publicvoidfizz(Runnable printFizz)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 3 != 0 || i % 5 == 0) { continue; } lock.lock(); try { while (i != value) { condition.await(); } printFizz.run(); value++; condition.signalAll(); } finally { lock.unlock(); } } }
// printBuzz.run() outputs "buzz". publicvoidbuzz(Runnable printBuzz)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 5 != 0 || i % 3 == 0) { continue; }
try { lock.lock(); while (i != value) { condition.await(); } printBuzz.run(); value++; condition.signalAll(); } finally { lock.unlock(); } } }
// printFizzBuzz.run() outputs "fizzbuzz". publicvoidfizzbuzz(Runnable printFizzBuzz)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 5 != 0 || i % 3 != 0) { continue; } lock.lock(); try { while (i != value) { condition.await(); } printFizzBuzz.run(); value++; condition.signalAll(); } finally { lock.unlock(); } } }
// printNumber.accept(x) outputs "x", where x is an integer. publicvoidnumber(IntConsumer printNumber)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 5 == 0 || i % 3 == 0) { continue; } lock.lock(); try { while (i != value) { condition.await(); } printNumber.accept(value); value++; condition.signalAll(); } finally { lock.unlock(); } } }
// printFizz.run() outputs "fizz". publicvoidfizz(Runnable printFizz)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 3 != 0 || i % 5 == 0) { continue; } synchronized (this) { while (i != value) { this.wait(); } printFizz.run(); value++; this.notifyAll(); } } }
// printBuzz.run() outputs "buzz". publicvoidbuzz(Runnable printBuzz)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 5 != 0 || i % 3 == 0) { continue; } synchronized (this) { while (i != value) { this.wait(); } printBuzz.run(); value++; this.notifyAll(); } } }
// printFizzBuzz.run() outputs "fizzbuzz". publicvoidfizzbuzz(Runnable printFizzBuzz)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 5 != 0 || i % 3 != 0) { continue; } synchronized (this) { while (i != value) { this.wait(); } printFizzBuzz.run(); value++; this.notifyAll(); } } }
// printNumber.accept(x) outputs "x", where x is an integer. publicvoidnumber(IntConsumer printNumber)throws InterruptedException { for (inti=1; i <= n; i++) { if (i % 5 == 0 || i % 3 == 0) { continue; } synchronized (this) { while (i != value) { this.wait(); } printNumber.accept(value); value++; this.notifyAll(); } } }