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

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

题目链接:点击打开链接

题目大意:略。

解题思路:略。

相关企业

  • 字节跳动
  • 微软(Microsoft)

AC 代码


// 方法1:CyclicBarrier
class FizzBuzz {
    private int n;
    private CyclicBarrier cb = 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 {
                cb.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 {
                cb.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 {
                cb.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 {
                cb.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
// 方法2:Semaphore
class FizzBuzz {
    private int n;
    private Semaphore number = new Semaphore(1);
    private Semaphore fizz = new Semaphore(0);
    private Semaphore buzz = new Semaphore(0);
    private Semaphore fizzbuzz = new Semaphore(0);
    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) {
                fizz.acquire();
                printFizz.run();
                number.release();
            }
        }
    }
    // 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) {
                buzz.acquire();
                printBuzz.run();
                number.release();
            }
        }
    }
    // 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) {
                fizzbuzz.acquire();
                printFizzBuzz.run();
                number.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++) {
            number.acquire();
            if (i % 3 != 0 && i % 5 != 0) {//开始打印
                printNumber.accept(i);
                number.release();
            } else if (i % 3 == 0 && i % 5 != 0) {//fizz开始打印
                fizz.release();
            } else if (i % 3 != 0 && i % 5 == 0) {//buzz开始打印
                buzz.release();
            } else {
                fizzbuzz.release();//fizzbuzz开始打印
            }
        }
    }
}
// 方法3:Semaphore
class FizzBuzz {
    private int n;
    private Semaphore semaphore = new Semaphore(1);
    private int cur = 1;
    public FizzBuzz(int n) {
        this.n = n;
    }
    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        while (true) {
            semaphore.acquire(1);
            try {
                // 原因就在这里,循环过程中如果打印的字符串个数已经满足要求,那么会使用return来返回,终止该方法的执行。
                // 但是咱们已经获取了信号量,那么在方法返回前需要释放该信号量,否则会导致其它线程一直等待,整个程序一直不结束。
                // Java语言中try-finally可以做到这一点,try-finally代码块也是常用的一种释放资源(IO流、数据库连接等)的方式。
                // 不是程序死循环,而是其它线程在wait,导致无法退出。
                if (cur > n) return;
                if (cur % 3 == 0 && cur % 5 != 0) {
                    cur++;
                    printFizz.run();
                }
            } finally {
                semaphore.release(1);
            }
        }
    }
    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while (true) {
            semaphore.acquire(1);
            try {
                if (cur > n) return;
                if (cur % 3 != 0 && cur % 5 == 0) {
                    cur++;
                    printBuzz.run();
                }
            } finally {
                semaphore.release(1);
            }
        }
    }
    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while (true) {
            semaphore.acquire(1);
            try {
                if (cur > n) return;
                if (cur % 3 == 0 && cur % 5 == 0) {
                    cur++;
                    printFizzBuzz.run();
                }
            } finally {
                semaphore.release(1);
            }
        }
    }
    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        while (true) {
            semaphore.acquire(1);
            try {
                if (cur > n) return;
                if (cur % 3 != 0 && cur % 5 != 0) {
                    printNumber.accept(cur);
                    cur++;
                }
            } finally {
                semaphore.release(1);
            }
        }
    }
}
// 方法4:Thread.yield()
class FizzBuzz {
    private int n;
    private volatile int state = 0; 
    public FizzBuzz(int n) {
        this.n = n;
    }
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (int i = 3; i <= n; i += 3) {   //只输出3的倍数(不包含15的倍数)
            if (i % 15 == 0) continue;   //15的倍数不处理,交给fizzbuzz()方法处理
            while (state != 3)
                Thread.yield();
            printFizz.run();
            state = 0;
        }
    }
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 5; i <= n; i += 5) {   //只输出5的倍数(不包含15的倍数)
            if (i % 15 == 0)    //15的倍数不处理,交给fizzbuzz()方法处理
                continue;
            while (state != 5)
                Thread.yield();
            printBuzz.run();
            state = 0;
        }
    }
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 15; i <= n; i += 15) {   //只输出15的倍数
            while (state != 15)
                Thread.yield();
            printFizzBuzz.run();
            state = 0;
        }
    }
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; ++i) {
            while (state != 0)
                Thread.yield();
            if (i % 3 != 0 && i % 5 != 0)
                printNumber.accept(i);
            else {
                if (i % 15 == 0)
                    state = 15;    //交给fizzbuzz()方法处理
                else if (i % 5 == 0)
                    state = 5;    //交给buzz()方法处理
                else
                    state = 3;    //交给fizz()方法处理
            }
        }
    }
}
// 方法5:ReentrantLock+Condition
class FizzBuzz {
    private int n;
    private ReentrantLock lock = new ReentrantLock();
    int state = 0;
    private Condition condition = lock.newCondition();
    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 += 3) {
            try {
                if (i % 3 == 0 && i % 5 == 0) continue;
                lock.lock();
                while (state != 3) {
                    condition.await();
                }
                printFizz.run();
                state = 0;
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }
    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 5; i <= n; i += 5) {
            try {
                if (i % 3 == 0 && i % 5 == 0) continue;
                lock.lock();
                while (state != 5) {
                    condition.await();
                }
                printBuzz.run();
                state = 0;
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }
    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 15; i <= n; i += 15) {
            try {
                lock.lock();
                while (state != 15) {
                    condition.await();
                }
                printFizzBuzz.run();
                state = 0;
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }
    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            try {
                lock.lock();
                while (state != 0) {
                    condition.await();
                }
                if (i % 3 != 0 && i % 5 != 0) {
                    printNumber.accept(i);
                } else {
                    if (i % 3 == 0 && i % 5 == 0) state = 15;
                    else if (i % 3 == 0) state = 3;
                    else if (i % 5 == 0) state = 5;
                    condition.signalAll();
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
// 方法6:ReentrantLock+Condition
class FizzBuzz {
    private int n;
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    private int i = 1;
    public FizzBuzz(int n) {
        this.n = n;
    }
    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        while (true) {
            try {
                lock.lock();
                if (i > n) {
                    return;
                }
                while (i % 3 != 0) {
                    condition.signalAll();
                    condition.await();
                    if (i > n) {
                        return;
                    }
                }
                printFizz.run();
                i++;
            } finally {
                condition.signalAll();
                lock.unlock();
            }
        }
    }
    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        while (true) {
            try {
                lock.lock();
                if (i > n) {
                    return;
                }
                while (i % 5 != 0) {
                    condition.signalAll();
                    condition.await();
                    if (i > n) {
                        return;
                    }
                }
                printBuzz.run();
                i++;
            } finally {
                condition.signalAll();
                lock.unlock();
            }
        }
    }
    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while (true) {
            try {
                lock.lock();
                if (i > n) {
                    return;
                }
                while (!(i % 3 == 0 && i % 5 == 0)) {
                    condition.signalAll();
                    condition.await();
                    if (i > n) {
                        return;
                    }
                }
                printFizzBuzz.run();
                i++;
            } finally {
                condition.signalAll();
                lock.unlock();
            }
        }
    }
    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        while (true) {
            try {
                lock.lock();
                if (i > n) {
                    return;
                }
                while (i % 3 == 0 || i % 5 == 0) {
                    condition.signalAll();
                    condition.await();
                    if (i > n) {
                        return;
                    }
                }
                printNumber.accept(i);
                i++;
            } finally {
                condition.signalAll();
                lock.unlock();
            }
        }
    }
}
目录
相关文章
|
2月前
|
JavaScript
力扣3333.找到初始输入字符串Ⅱ
【10月更文挑战第9天】力扣3333.找到初始输入字符串Ⅱ
36 1
|
3月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
C++
Leetcode第43题(字符串相乘)
本篇介绍了一种用C++实现的字符串表示的非负整数相乘的方法,通过逆向编号字符串,将乘法运算转化为二维数组的累加过程,最后处理进位并转换为字符串结果,解决了两个大数相乘的问题。
25 9
|
2月前
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
20 0
|
2月前
【LeetCode 22】459.重复的子字符串
【LeetCode 22】459.重复的子字符串
30 0
|
2月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
20 0
|
2月前
【LeetCode 19】541.反转字符串II
【LeetCode 19】541.反转字符串II
21 0
|
2月前
【LeetCode 18】6.2.反转字符串
【LeetCode 18】6.2.反转字符串
16 0
|
4月前
|
存储 算法
LeetCode第43题字符串相乘
LeetCode第43题"字符串相乘"的解题方法,通过使用数组存储乘积并处理进位,避免了字符串转换数字的复杂性,提高了算法效率。
LeetCode第43题字符串相乘
|
4月前
|
算法 Java
LeetCode第28题找出字符串中第一个匹配项的下标
这篇文章介绍了LeetCode第28题"找出字符串中第一个匹配项的下标"的两种解法:暴力解法和KMP算法,并解释了KMP算法通过构建前缀表来提高字符串搜索的效率。
LeetCode第28题找出字符串中第一个匹配项的下标