码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-1195-交替打印字符串

时间:2020-07-09 00:49:01      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:block   ack   new   ++   trace   exce   static   vat   ntb   

题目描述:

技术图片

 

 技术图片

 

 方法一:纯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-交替打印字符串

标签:block   ack   new   ++   trace   exce   static   vat   ntb   

原文地址:https://www.cnblogs.com/oldby/p/13269955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!