leetcode-1195-交替打印字符串

Posted 真不知道叫啥好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-1195-交替打印字符串相关的知识,希望对你有一定的参考价值。

题目描述:

 

 

 

 方法一:纯volatile实现

class FizzBuzz {
    private int n;
    private volatile int f = 0;
    private volatile int b = 0;
    private volatile int fb = 0;
    private volatile int nm = 1;
    private volatile int i = 1;

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

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        while(i<=n){
            while(f == 0&&i<=n){}
            if(i > n){
                break;
            }
            printFizz.run();
            i++;
            nm = 1;
            f = 0;
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while(i<=n){
            while(b == 0&&i<=n){}
            if(i > n){
                break;
            }
            printBuzz.run();
            i++;
            nm = 1;
            b = 0;
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while(i<=n){
            while(fb == 0&&i<=n){}
            if(i > n){
                break;
            }
            printFizzBuzz.run();
            i++;
            nm = 1;
            fb = 0;
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        while(i<=n){
            while(nm == 0&&i<=n){}
            if(i > n){
                break;
            }
            if(i%3 != 0 && i%5 != 0){
                printNumber.accept(i);
                i++;
            }else{
                nm = 0; // number block
                if(i%3==0&&i%15!=0)
                    f = 1;  //fizz unblock 
                    
                if(i%5==0&&i%15!=0)
                    b = 1;  //buzz unblock 
                    
                if(i%15==0)
                    fb = 1;  //fizzbuzz unblock
            }
        }
    }
}

        
        

方法二:cyclicBarrier:

class FizzBuzz {
    private int n;
    private static CyclicBarrier barrier = new CyclicBarrier(4);

    public FizzBuzz(int n) {
        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();
            }
        }
    }
}

        
        

方法三:最快

class FizzBuzz {
    private int n;
        
    private Semaphore fSema = new Semaphore(0);
    private Semaphore bSema = new Semaphore(0);
    private Semaphore fbSema = new Semaphore(0);
    private Semaphore nSema = new Semaphore(1);

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

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (int i = 3; i <= n; i = i + 3) {
            if (i % 5 != 0) {
                fSema.acquire();
                printFizz.run();
                nSema.release();
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 5; i <= n; i = i + 5) {
            if (i % 3 != 0) {
                bSema.acquire();
                printBuzz.run();
                nSema.release();
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 15; i <= n; i = i + 15) {
            fbSema.acquire();
            printFizzBuzz.run();
            nSema.release();
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            nSema.acquire();
            if (i % 3 == 0 && i % 5 == 0) {
                fbSema.release();
            }else if (i % 3 == 0) {
                fSema.release();
            }else if (i % 5 == 0) {
                bSema.release();
            }else {
                printNumber.accept(i);
                nSema.release();
            }
        }
    }
}
        
        

 

以上是关于leetcode-1195-交替打印字符串的主要内容,如果未能解决你的问题,请参考以下文章

多线程面试题之三线程按顺序交替打印ABC的方法

Java面试题:交替打印字符串

Python实现线程交替打印字符串

三个线程交替打印ABC100次问题思考

LeetCode Go 并发题详解:交替打印字符串

佳能IP4200喷墨打印机出现6502错误代码,黄灯和绿灯交替闪19次是啥原因?怎么修理?