leetcode 1195. alternately print string

leetcode 1195. alternately print string 

to write a 1 to n from the figure represent program output string, however: 

if the number can be divisible by 3, the output of "fizz". 
If this number can be divisible by 5, the output of "buzz". 
If this number can be simultaneously divisible by 3 and 5, the output "fizzbuzz". 
For example, when n = 15, output: 1, 2, fizz, 4 , buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz. 

Suppose there is such a class: 

class {FizzBuzz 
  public FizzBuzz (n-int) {...} // constructor 
  public void Fizz (printFizz) {...} // Output only "Fizz" 
  public void Buzz (printBuzz) {.. .} // Output only "Buzz" 
  public void FizzBuzz (printFizzBuzz) {...} // Output only "FizzBuzz" 
  public void Number (printNumber) {...

A thread calls Fizz () to determine whether or not divisible by 3, if so, output fizz. 
Buzz thread B calls () can be divisible by 5 to determine whether, and if so, the output buzz. 
The calling thread FizzBuzz C () to determine whether simultaneously divisible by 3 and 5, if so, output fizzbuzz. 
The thread D call number () is used to achieve either not be divisible by 3 5 divisible numbers. 

Zero Solution: do not need anything, we directly while loop to repeat determination condition is satisfied, the problem is due to the num is not volatile, and can not update, many threads are meaningless cycle of death (beyond the time limit) 
class FizzBuzz { 
    Private int n-; 
    Private int NUM; 

    public FizzBuzz (n-int) { 
        this.n n-=; 
    } 

    // printFizz.run () Outputs "Fizz." 
    public void Fizz (the Runnable printFizz) throws InterruptedException { 
        the while (NUM <= n-) { 
            IF (% NUM. 3 == 0 && NUM = 0. 5%!) { 
                printFizz.run (); 
                NUM ++;  
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while(num<=n){
            if(num%3!=0 && num%5==0){
                printBuzz.run();
                num++;
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while(num<=n){
            if(num%3==0 && num%5==0){
                printFizzBuzz.run();
                num++;
            }
        }
    }

    PrintNumber.accept // (X) Outputs "X", WHERE X IS AN Integer. 
    Public void Number (IntConsumer printNumber) throws InterruptedException { 
        the while (NUM <= n-) { 
            IF (% NUM. 3! = 0 && NUM. 5%! 0 =) { 
                printNumber.accept (NUM); 
                NUM ++; 
            } 
        } 
    } 
} 

Method a: volatile (for the same time may have multiple threads to read a variable, but necessarily write only one thread variables, there is no use of volatile problems) 
class FizzBuzz { 
    Private n-int; 
    Private volatile int NUM; 

    public FizzBuzz (n-int) { 
        this.n n-=; 
    } 

    // printFizz.run () Outputs "Fizz."  
    public void Fizz (the Runnable printFizz) throws InterruptedException {
        the while (NUM <= n-) { 
            IF ( num% 3 == 0 && num% 5!=0){
                printFizz.run();
                num++;
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while(num<=n){
            if(num%3!=0 && num%5==0){
                printBuzz.run();
                num++;
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while(num<=n){
            if(num%3==0 && num%5==0){
                printFizzBuzz.run();
                NUM ++; 
            } 
        } 
    } 

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void Number (IntConsumer printNumber) throws InterruptedException { 
        the while (NUM <= n-) { 
            IF (NUM%. 3 = 0 && NUM%. 5 = 0!!) { 
                printNumber.accept (NUM); 
                NUM ++; 
            } 
        } 
    } 
} 

Solution II: Since it is volatile, then the inevitable class AtomicInteger atomic operations are also possible (code is almost exactly where it is no longer demonstrate) 



Solution three: synchronized + volatile (we know that at some point, there is only one inevitable operation thread is significant, so we can introduce synchronized to allow other threads wait (), that time in the Notify) 

class FizzBuzz { 
    Private int the n-; 
    Private volatile int NUM; 
    Private Lock Object = new new Object (); 

    public FizzBuzz ( int n) { 
        this.n n-=; 
    } 

    / / printFizz.run () outputs "fizz" .
    public void fizz(Runnable printFizz) throws InterruptedException {
        synchronized(lock){
            while(num<=n){
                if(num%3==0 && num%5!=0){
                    printFizz.run();
                    num++;
                    lock.notifyAll();
                }else{
                    lock.wait();
                }
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        synchronized(lock){
            while(num<=n){
                if(num%3!=0 && num%5==0){
                    printBuzz.run();
                    num++;
                    lock.notifyAll();
                }else{
                    lock.wait();
                }
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        synchronized(lock){
            while(num<=n){
                if(num%3==0 && num%5==0){
                    printFizzBuzz.run();
                    num++;
                    lock.notifyAll();
                }else{
                    lock.wait();
                }
            } 
        } 
class FizzBuzz {
    }

    PrintNumber.accept // (X) Outputs "X", WHERE X IS AN Integer. 
    Public void Number (IntConsumer printNumber) throws InterruptedException { 
        the synchronized (Lock) { 
            the while (NUM <= n-) { 
                IF (% NUM. 3! = 0 ! = 0 && NUM. 5%) { 
                    printNumber.accept (NUM); 
                    NUM ++; 
                    lock.notifyAll (); 
                } the else { 
                    lock.wait (); 
                } 
            } 
        } 
    } 
} 

Solution four: volatile + ReentrantLock (synchronized since it was possible that lock must also be possible, although the two brother is almost exactly the same, but in order to review it, we can also look at) 
    Private int the n-; 
    Private volatile int NUM;
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public FizzBuzz(int n) {
        this.n = n;
    }

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        while(num<=n){
            lock.lock();
            try{
                if(num%3==0 && num%5!=0){
                    printFizz.run();
                    num++;
                    condition.signalAll();
                }else{
                    condition.await();
                }
            }finally{
                lock.unlock();
            }   
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while(num<=n){
            lock.lock();
            try{
                if(num%3!=0 && num%5==0){
                    printBuzz.run();
                    num++;
                    condition.signalAll();
                }else{
                    condition.await();
                }
            }finally{
                lock.unlock();
            }   
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while(num<=n){
            lock.lock();
            try{
                if(num%3==0 && num%5==0){
                    printFizzBuzz.run();
                    num++;
                    condition.signalAll();
                }else{
                    condition.await();
                }
            }finally{
                lock.unlock();
            }   
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
       the while (NUM <= n-) { 
            Lock.lock ();
            try{
                if(num%3!=0 && num%5!=0){
                    printNumber.accept (NUM); 
                    NUM ++; 
                    condition.signalAll (); 
                } the else { 
                    Condition.await (); 
                } 
            } {the finally 
                lock.unlock (); 
            }    
        } 
    } 
} 











The above conventional methods are used mutex solve the problem, now let's use a little something to solve the problem directly, it has what it 
countDownLatch CyclicBarrier semaphore exchanger 

Solution of the five: 

Private static CyclicBarrier Barrier = new new CyclicBarrier (4); 

    public FizzBuzz (the n-int) { 
        this.n n-=; 
    } 

    // printFizz.run () Outputs "Fizz."
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 != 0) {
                printFizz.run();
            }
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i % 3 != 0 && i % 5 == 0) {
                printBuzz.run();
            }
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                printFizzBuzz.run();
            }
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i % 3 != 0 && i % 5 != 0) {
                printNumber.accept(i);
            }
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }



    

  

leetcode 1195. alternately print string
to write a 1 to n from the figure represent program output string, however:
if the number can be divisible by 3, the output of "fizz". If this number can be divisible by 5, the output of "buzz". If this number can be simultaneously divisible by 3 and 5, the output "fizzbuzz". For example, when n = 15, output: 1, 2, fizz, 4 , buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz.
Suppose there is such a class:
class {public FizzBuzz FizzBuzz (n-int) {...} public void Fizz // constructor (printFizz) {...} // Output only "Fizz" public void Buzz (printBuzz) {.. .} // only output "buzz" public void fizzbuzz (printFizzBuzz) {...} // only output "fizzbuzz" public void number (printNumber) {...} // only output the numbers} you achieve a four threads of a multithreaded version FizzBuzz, FizzBuzz instance is the same as four threads:
A thread calls Fizz () to determine whether or not divisible by 3, if so, output fizz. Buzz thread B calls () can be divisible by 5 to determine whether, and if so, the output buzz. The calling thread FizzBuzz C () to determine whether simultaneously divisible by 3 and 5, if so, output fizzbuzz. The thread D call number () is used to achieve either not be divisible by 3 5 divisible numbers.
Zero Solution: do not need anything, we directly while loop to repeat determination condition is satisfied, the problem is due to the num is not volatile, and can not update, many threads are meaningless cycle of death (beyond the time limit) class FizzBuzz {private int n-; Private int NUM;
    public FizzBuzz (n-int) = {n-this.n;}
    // printFizz.run () Outputs "Fizz" public void Fizz (the Runnable printFizz) throws InterruptedException {the while (NUM <= n-) {. if (num% 3 == 0 && num% 5 = 0!) {printFizz.run (); num ++;}}}



Solution a: volatile (because the same time there may be multiple threads to read a variable, but inevitably there is only one thread writes a variable, so the use of volatile no problem) class FizzBuzz {Private int the n-; Private volatile int NUM;
    public FizzBuzz ( n-int) = {n-this.n;}
    . // printFizz.run () Outputs "Fizz" public void Fizz (the Runnable printFizz) throws InterruptedException {the while (NUM <= n-) {IF (% NUM. 3 == 0 && ! NUM = 0. 5%) {printFizz.run (); NUM ++;}}}
    // printBuzz.run () Outputs "Buzz" public void Buzz (the Runnable printBuzz) throws InterruptedException {the while (NUM <= n-) {IF. (! num% 3 = 0 && num% 5 == 0) {printBuzz.run (); num ++;}}}
    // printFizzBuzz.run () outputs "fizzbuzz" . Public void fizzbuzz (Runnable printFizzBuzz) throws InterruptedException {while (num <= n) {if (num% 3 == 0 && num% 5 == 0) {printFizzBuzz.run (); NUM ++;}}}
    . // printNumber.accept (X) Outputs "X", WHERE X IS AN public void Integer Number (IntConsumer printNumber) throws InterruptedException {the while (NUM <= n-) {IF (NUM. 3% !! = 0 && num% 5 = 0) {printNumber.accept (num); num ++;}}}}
Solution II: Since it is volatile, then AtomicInteger class must also be an atomic operation (code here almost identical not demonstrated)


Solution three: synchronized + volatile (we know that at some point, must operate only one thread is meaningful, so we can introduce synchronized to allow other threads wait (), at that time notify)
class FizzBuzz {    private int n;    private volatile int num;    private Object lock = new Object();
    public FizzBuzz(int n) {        this.n = n;    }
    // printFizz.run() outputs "fizz".    public void fizz(Runnable printFizz) throws InterruptedException {        synchronized(lock){            while(num<=n){                if(num%3==0 && num%5!=0){                    printFizz.run();                    num++;                    lock.notifyAll();                }else{                    lock.wait();                }            }        }    }
    // printBuzz.run() outputs "buzz".    public void buzz(Runnable printBuzz) throws InterruptedException {        synchronized(lock){            while(num<=n){                if(num%3!=0 && num%5==0){                    printBuzz.run();                    num++;                    lock.notifyAll();                }else{                    lock.wait();                }            }        }    }
    // printFizzBuzz.run() outputs "fizzbuzz".    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {        synchronized(lock){            while(num<=n){                if(num%3==0 && num%5==0){                    printFizzBuzz.run();                    num++;                    lock.notifyAll();                }else{                    lock.wait();                }            }        }    }
    // printNumber.accept (x) outputs "x ", where x is an integer. Public void number (IntConsumer printNumber) throws InterruptedException {synchronized (lock) {while (num <= n) {if (num% 3! = 0 ! && num% 5 = 0) {printNumber.accept (num); num ++; lock.notifyAll ();} else {lock.wait ();}}}}}
Solution four: volatile + ReentrantLock (synchronized since it was possible that lock must also be possible, although the two brother is almost exactly the same, but in order to review it, we can also look at) class FizzBuzz {private int n; private volatile int num; ReentrantLock lock = new ReentrantLock (); = lock.newCondition for condition Condition for condition Condition ();
    public FizzBuzz (n-int) = {n-this.n;}
    // printFizz.run() outputs "fizz".    public void fizz(Runnable printFizz) throws InterruptedException {        while(num<=n){            lock.lock();            try{                if(num%3==0 && num%5!=0){                    printFizz.run();                    num++;                    condition.signalAll();                }else{                    condition.await();                }            }finally{                lock.unlock();            }           }    }
    // printBuzz.run() outputs "buzz".    public void buzz(Runnable printBuzz) throws InterruptedException {        while(num<=n){            lock.lock();            try{                if(num%3!=0 && num%5==0){                    printBuzz.run();                    num++;                    condition.signalAll();                }else{                    condition.await();                }            }finally{                lock.unlock();            }           }    }
    // printFizzBuzz.run() outputs "fizzbuzz".    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {        while(num<=n){            lock.lock();            try{                if(num%3==0 && num%5==0){                    printFizzBuzz.run();                    num++;                    condition.signalAll();                }else{                    condition.await();                }            }finally{                lock.unlock();            }           }    }
    // printNumber.accept (x) outputs "x ", where x is an integer public void number (IntConsumer printNumber) throws InterruptedException {while (num <= n) {lock.lock ();. Try {if (num% 3 !! = 0 && num% 5 = 0) {printNumber.accept (num); num ++; condition.signalAll ();} else {condition.await ();}} finally {lock.unlock ();}}}}










the above methods are the use of the traditional mutex to solve the problem, so now let's use a little something to solve the problem directly, it has what it countDownLatch CyclicBarrier semaphore exchanger
Solution of the five:
Private static CyclicBarrier Barrier = new new CyclicBarrier (4 );
    public FizzBuzz (n-int) = {n-this.n;}
    // printFizz.run() outputs "fizz".    public void fizz(Runnable printFizz) throws InterruptedException {        for (int i = 1; i <= n; i++) {            if (i % 3 == 0 && i % 5 != 0) {                printFizz.run();            }            try {                barrier.await();            } catch (BrokenBarrierException e) {                e.printStackTrace();            }        }    }
    // printBuzz.run() outputs "buzz".    public void buzz(Runnable printBuzz) throws InterruptedException {        for (int i = 1; i <= n; i++) {            if (i % 3 != 0 && i % 5 == 0) {                printBuzz.run();            }            try {                barrier.await();            } catch (BrokenBarrierException e) {                e.printStackTrace();            }        }    }
    // printFizzBuzz.run() outputs "fizzbuzz".    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {        for (int i = 1; i <= n; i++) {            if (i % 3 == 0 && i % 5 == 0) {                printFizzBuzz.run();            }            try {                barrier.await();            } catch (BrokenBarrierException e) {                e.printStackTrace();            }        }    }
    // printNumber.accept(x) outputs "x", where x is an integer.    public void number(IntConsumer printNumber) throws InterruptedException {        for (int i = 1; i <= n; i++) {            if (i % 3 != 0 && i % 5 != 0) {                printNumber.accept(i);            }            try {                barrier.await();            } catch (BrokenBarrierException e) {                e.printStackTrace();            }        }    }


    

Guess you like

Origin www.cnblogs.com/helloworldmybokeyuan/p/12030329.html