Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. class FizzBuzz {
  2.     private int n;
  3.     private int counter;
  4.     private boolean complete;
  5.  
  6.     public FizzBuzz(int n) {
  7.         this.n = n;
  8.         this.counter = 1;
  9.         this.complete = false;
  10.     }
  11.  
  12.     // printFizz.run() outputs "fizz".
  13.     public synchronized void fizz(Runnable printFizz) throws InterruptedException {
  14.         while((counter % 3 != 0 || counter % 5 == 0) && !complete) {
  15.             wait();
  16.         }
  17.                
  18.         while (!complete) {
  19.             printFizz.run();
  20.             incrementAndValidateCounter();            
  21.             if (complete) {
  22.                 notifyAll();
  23.                 break;
  24.             }
  25.            
  26.             boolean first = true;
  27.            
  28.             while((counter % 3 != 0 || counter % 5 == 0) && !complete) {                
  29.                 if (first) {
  30.                     notifyAll();
  31.                     first = false;
  32.                 }
  33.                 wait();
  34.             }
  35.         }
  36.     }
  37.  
  38.     // printBuzz.run() outputs "buzz".
  39.     public synchronized void buzz(Runnable printBuzz) throws InterruptedException {
  40.         while((counter % 3 == 0 || counter % 5 != 0) && !complete) {
  41.             wait();
  42.         }
  43.                
  44.         while (!complete) {
  45.             printBuzz.run();
  46.             incrementAndValidateCounter();  
  47.             if (complete) {
  48.                 notifyAll();
  49.                 break;
  50.             }
  51.            
  52.             boolean first = true;
  53.            
  54.             while((counter % 3 == 0 || counter % 5 != 0) && !complete) {
  55.                 if (first) {
  56.                     notifyAll();
  57.                     first = false;
  58.                 }
  59.                 wait();
  60.             }
  61.         }        
  62.     }
  63.  
  64.     // printFizzBuzz.run() outputs "fizzbuzz".
  65.     public synchronized void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
  66.         while((counter % 3 != 0 || counter % 5 != 0) && !complete) {
  67.             wait();
  68.         }
  69.        
  70.         while (!complete) {
  71.             printFizzBuzz.run();
  72.             incrementAndValidateCounter();            
  73.             if (complete) {
  74.                 notifyAll();
  75.                 break;
  76.             }
  77.            
  78.             boolean first = true;
  79.            
  80.             while((counter % 3 != 0 || counter % 5 != 0) && !complete) {
  81.                 if (first) {
  82.                     notifyAll();
  83.                     first = false;
  84.                 }
  85.                 wait();
  86.             }
  87.         }
  88.     }
  89.  
  90.     // printNumber.accept(x) outputs "x", where x is an integer.
  91.     public synchronized void number(IntConsumer printNumber) throws InterruptedException {
  92.         while((counter % 3 == 0 || counter % 5 == 0) && !complete) {
  93.             wait();
  94.         }
  95.        
  96.         while (!complete) {
  97.             printNumber.accept(counter);
  98.             incrementAndValidateCounter();
  99.                        
  100.             if (complete) {
  101.                 notifyAll();
  102.                 break;
  103.             }
  104.            
  105.             boolean first = true;
  106.  
  107.             while((counter % 3 == 0 || counter % 5 == 0) && !complete) {
  108.                 if (first) {
  109.                     notifyAll();
  110.                     first = false;
  111.                 }
  112.                 wait();
  113.             }
  114.         }        
  115.     }
  116.    
  117.     private void incrementAndValidateCounter() {
  118.         ++counter;
  119.         if (counter > n) {
  120.             complete = true;
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement