leetcode 1114
leetcode 1114:按需打印
解法一:Semaphore
class Foo {private Semaphore first = new Semaphore(1);private Semaphore second = new Semaphore(0);private Semaphore third = new Semaphore(0);public Foo() {}public void first(Runnable printFirst) throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.first.acquire();printFirst.run();second.release();}public void second(Runnable printSecond) throws InterruptedException {// printSecond.run() outputs "second". Do not change or remove this line.second.acquire();printSecond.run();third.release();}public void third(Runnable printThird) throws InterruptedException {// printThird.run() outputs "third". Do not change or remove this line.third.acquire();printThird.run();first.release();}
}
解法二 CountDownLatch
class Foo {private CountDownLatch second = new CountDownLatch(1);private CountDownLatch third = new CountDownLatch(1);public Foo() {}public void first(Runnable printFirst) throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.printFirst.run();second.countDown();}public void second(Runnable printSecond) throws InterruptedException {// printSecond.run() outputs "second". Do not change or remove this line.second.await();printSecond.run();third.countDown();}public void third(Runnable printThird) throws InterruptedException {// printThird.run() outputs "third". Do not change or remove this line.third.await();printThird.run();}
}
解法三 synchronized
class Foo {private boolean first = false;private boolean second = false;private static Object lock = new Object();public Foo() {}public void first(Runnable printFirst) throws InterruptedException {synchronized (lock) {// printFirst.run() outputs "first". Do not change or remove this line.printFirst.run();first = true;lock.notifyAll();}}public void second(Runnable printSecond) throws InterruptedException {synchronized (lock) {while (!first) {lock.wait();}// printSecond.run() outputs "second". Do not change or remove this line.printSecond.run(); second = true;lock.notifyAll();}}public void third(Runnable printThird) throws InterruptedException {synchronized (lock) {while (!second) {lock.wait();}// printThird.run() outputs "third". Do not change or remove this line.printThird.run();}}
}
leetcode 1114相关推荐
- LeetCode 1114. Print in Order--Java解法--并发问题
此文首发于我的个人博客:LeetCode 1114. Print in Order–Java解法–并发问题 - zhang0peter的个人博客 LeetCode题解专栏:LeetCode题解 Lee ...
- 2022-4-8 Leetcode 1114.按顺序打印
原来直接用信号量就好了 #include <semaphore.h>class Foo {protected:sem_t firstJobDone;sem_t secondJobDone; ...
- LeetCode 1114 按序打印
https://leetcode-cn.com/problems/print-in-order/ 解决方案 class Foo {private AtomicInteger jobDone = new ...
- LeetCode 1114. 按序打印
我们提供了一个类: public class Foo {public void first() { print("first"); }public void second() { ...
- LeetCode 所有题目总结
文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...
- LeetCode 多线程 1114. 按序打印
1114. 按序打印 Ideas 并发执行问题是多线程要解决的经典问题,此题是典型的执行屏障问题,因此我们需要构造几把锁来确保执行顺序. 题目要求按顺序依次执行三个方法,为了保证线程的执行顺序,可以在 ...
- C#LeetCode刷题-多线程
本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-多线程 | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode多线程类算法题汇总. ...
- LeetCode:交替打印【1115】
LeetCode:交替打印[1115] 题目描述 我们提供一个类: class FooBar { public void foo() {for (int i = 0; i < n; i++) { ...
- Java算法:LeetCode算法Java版合集1111-1588题
1111. 有效括号的嵌套深度 题目描述 有效括号字符串 仅由 "(" 和 ")" 构成,并符合下述几个条件之一: 空字符串 连接,可以记作 AB(A 与 B ...
- 面试刷题LeetCode经典100道
准备面试刷题,100道经典LeetCode题目奉上. 题号 英文题名 中文题名 难度 likes 数 标签 1 Two Sum 两数之和 Easy 11712 数组,哈希表 2 Add Two Num ...
最新文章
- 哲学是什么?(选自:苏菲的世界)
- VS.NET2003 开发环境 生成样式表 和 自动书写HTML对象模型
- 皮一皮:秀恩爱死的快啊!!!!
- Spring为什么建议使用构造器来注入?
- 扩展js string 方法
- 由神经网络的迭代次数计算输出值并评价网络性能
- java队列_RPC远程调用和消息队列MQ的区别
- boost::iterator_facade用法的测试程序
- c++向mysql通信_C++连接MySQL
- ACwing 3. 完全背包问题(DP)
- git分支建立删除,打tag
- CMM关键过程域剖析:需求管理
- Tokyo Tyrant优势
- 如何使用Burp Suite代理
- win10更新助手_快升级!win10精简版不到10G,比win7还干净流畅,无需更新!
- 【Python+Selenium】输入关键词自动下载论文
- linux .sh文件详解,sh命令(sh命令详解)
- 【渝粤教育】电大中专中医基础知识 (3)作业 题库
- 如何在 Mac 上更改鼠标光标颜色?
- 中国大学慕课第8周测验