LeetCode-1195.交替打印字符串(多线程)

LeetCode 题目描述

编写一个可以从 1 到 n 输出代表这个数字的字符串的程序,但是:

  • 如果这个数字可以被 3 整除,输出 “fizz”。
  • 如果这个数字可以被 5 整除,输出 “buzz”。
  • 如果这个数字可以同时被 3 和 5 整除,输出 “fizzbuzz”。

例如,当 n = 15,输出: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz。

假设有这么一个类:

class FizzBuzz {
  public FizzBuzz(int n) { ... }               // constructor
  public void fizz(printFizz) { ... }          // only output "fizz"
  public void buzz(printBuzz) { ... }          // only output "buzz"
  public void fizzbuzz(printFizzBuzz) { ... }  // only output "fizzbuzz"
  public void number(printNumber) { ... }      // only output the numbers
}

请你实现一个有四个线程的多线程版  FizzBuzz, 同一个 FizzBuzz 实例会被如下四个线程使用:

  • 线程 A 将调用 fizz() 来判断是否能被 3 整除,如果可以,则输出 fizz。
  • 线程 B 将调用 buzz() 来判断是否能被 5 整除,如果可以,则输出 buzz。
  • 线程 C 将调用 fizzbuzz() 来判断是否同时能被 3 和 5 整除,如果可以,则输出 fizzbuzz。
  • 线程 D 将调用 number() 来实现输出既不能被 3 整除也不能被 5 整除的数字。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fizz-buzz-multithreaded
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

整体思考思路为:
4 个线程并发执行

①.打印数字的线程控制着其他 3 个线程的执行,每次打印数字线程 number 方法执行后判断数字整除情况唤醒对应线程。但是该处我们考虑是否有这种情况,number 方法唤醒 fizz 方法,fizz 方法执行后其实直接可以执行 buzz 方法了,如果我们通过 number 方法过渡会出现多余的操作。(参考 Solution1)
②.每个线程判断整除情况唤醒对应的线程。(最终考虑停止程序时释放所有许可时判断较为复杂)
③.使用单个信号量+for 循环轮询判断,满足条件执行,不满足条件则释放许可后让其他方法进行判断。(参考 Solution2,该实现考虑-如果某方法耗时较长时性能问题)

Solution1.Semaphore + 整型变量

class FizzBuzz {
    private int n;

    Semaphore fizzSem = new Semaphore(0);
    Semaphore buzzSem = new Semaphore(0);
    Semaphore fizzBuzzSem = new Semaphore(0);
    Semaphore numSem = new Semaphore(1);

    private int i;

    public FizzBuzz(int n) {
        //从 1 开始
        this.i = 1;
        this.n = n;
    }

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        while (true) {
            fizzSem.acquire();
            if (i > n) {
                break;
            }
            printFizz.run();
            i++;
            numSem.release();
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while (true) {
            buzzSem.acquire();
            if (i > n) {
                break;
            }
            printBuzz.run();
            i++;
            numSem.release();
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while (true) {
            fizzBuzzSem.acquire();
            if (i > n) {
                break;
            }
            printFizzBuzz.run();
            i++;
            numSem.release();
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        ///主要是这儿执行完后其他的还在等,所以会超时,这个时候释放掉所有的就行了。
        while (i <= n) {
            numSem.acquire();
            if (i % 3 == 0 && i % 5 == 0) {
                fizzBuzzSem.release();
            } else if (i % 3 == 0) {
                fizzSem.release();
            } else if (i % 5 == 0) {
                buzzSem.release();
            } else {
                if (i <= n) {
                    printNumber.accept(i);
                }
                i++;
                numSem.release();
            }
        }
        release(); // 最终释放许可
    }

    private void release() {
        fizzBuzzSem.release();
        buzzSem.release();
        fizzSem.release();
    }
}

Solution2.Semaphore + for 循环轮询判断

class FizzBuzz {
    private Semaphore semaphore = new Semaphore(1);
    private int curNum = 1;
    private int n;

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

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (; ; ) {
            this.semaphore.acquire(1);
            try {
                if (this.curNum > n) {
                    return;
                }

                if ((this.curNum % 3 == 0) && (this.curNum % 5 != 0)) {
                    printFizz.run();
                    this.curNum++;
                }
            } finally {
                // Release the permit anyway.
                this.semaphore.release(1);
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (; ; ) {
            this.semaphore.acquire(1);
            try {
                if (this.curNum > n) {
                    return;
                }

                if ((this.curNum % 3 != 0) && (this.curNum % 5 == 0)) {
                    printBuzz.run();
                    this.curNum++;
                }
            } finally {
                this.semaphore.release(1);
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (; ; ) {
            this.semaphore.acquire(1);
            try {
                if (this.curNum > n) {
                    return;
                }

                if ((this.curNum % 3 == 0) && (this.curNum % 5 == 0)) {
                    printFizzBuzz.run();
                    this.curNum++;
                }
            } finally {
                this.semaphore.release(1);
            }
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (; ; ) {
            this.semaphore.acquire(1);

            try {
                if (this.curNum > n) {
                    return;
                }

                if ((this.curNum % 3 != 0) && (this.curNum % 5 != 0)) {
                    printNumber.accept(this.curNum);
                    this.curNum++;
                }
            } finally {
                this.semaphore.release(1);
            }
        }
    }
}

参考

LeetCode-1195.交替打印字符串(多线程)LeetCode-1195.交替打印字符串(多线程) 2.wa 发布了250 篇原创文章 · 获赞 55 · 访问量 38万+ 他的留言板 关注
上一篇:laravel sql mode only_full_group_by 解决小记


下一篇:CSS Writing Modes