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相关推荐

  1. LeetCode 1114. Print in Order--Java解法--并发问题

    此文首发于我的个人博客:LeetCode 1114. Print in Order–Java解法–并发问题 - zhang0peter的个人博客 LeetCode题解专栏:LeetCode题解 Lee ...

  2. 2022-4-8 Leetcode 1114.按顺序打印

    原来直接用信号量就好了 #include <semaphore.h>class Foo {protected:sem_t firstJobDone;sem_t secondJobDone; ...

  3. LeetCode 1114 按序打印

    https://leetcode-cn.com/problems/print-in-order/ 解决方案 class Foo {private AtomicInteger jobDone = new ...

  4. LeetCode 1114. 按序打印

    我们提供了一个类: public class Foo {public void first() { print("first"); }public void second() { ...

  5. LeetCode 所有题目总结

    文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...

  6. LeetCode 多线程 1114. 按序打印

    1114. 按序打印 Ideas 并发执行问题是多线程要解决的经典问题,此题是典型的执行屏障问题,因此我们需要构造几把锁来确保执行顺序. 题目要求按顺序依次执行三个方法,为了保证线程的执行顺序,可以在 ...

  7. C#LeetCode刷题-多线程

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-多线程 | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode多线程类算法题汇总. ...

  8. LeetCode:交替打印【1115】

    LeetCode:交替打印[1115] 题目描述 我们提供一个类: class FooBar { public void foo() {for (int i = 0; i < n; i++) { ...

  9. Java算法:LeetCode算法Java版合集1111-1588题

    1111. 有效括号的嵌套深度 题目描述 有效括号字符串 仅由 "(" 和 ")" 构成,并符合下述几个条件之一: 空字符串 连接,可以记作 AB(A 与 B ...

  10. 面试刷题LeetCode经典100道

    准备面试刷题,100道经典LeetCode题目奉上. 题号 英文题名 中文题名 难度 likes 数 标签 1 Two Sum 两数之和 Easy 11712 数组,哈希表 2 Add Two Num ...

最新文章

  1. 哲学是什么?(选自:苏菲的世界)
  2. VS.NET2003 开发环境 生成样式表 和 自动书写HTML对象模型
  3. 皮一皮:秀恩爱死的快啊!!!!
  4. Spring为什么建议使用构造器来注入?
  5. 扩展js string 方法
  6. 由神经网络的迭代次数计算输出值并评价网络性能
  7. java队列_RPC远程调用和消息队列MQ的区别
  8. boost::iterator_facade用法的测试程序
  9. c++向mysql通信_C++连接MySQL
  10. ACwing 3. 完全背包问题(DP)
  11. git分支建立删除,打tag
  12. CMM关键过程域剖析:需求管理
  13. Tokyo Tyrant优势
  14. 如何使用Burp Suite代理
  15. win10更新助手_快升级!win10精简版不到10G,比win7还干净流畅,无需更新!
  16. 【Python+Selenium】输入关键词自动下载论文
  17. linux .sh文件详解,sh命令(sh命令详解)
  18. 【渝粤教育】电大中专中医基础知识 (3)作业 题库
  19. 如何在 Mac 上更改鼠标光标颜色?
  20. 中国大学慕课第8周测验

热门文章

  1. java服务端监控平台设计
  2. 调用高德api 实现地点搜索,且获取经纬度
  3. mysql实验报告4_实验四∶数据库安全性实验报告.doc
  4. linux中静态IP的配置及检测
  5. 如何书写批处理文件?(批处理文件的介绍及编写规范)
  6. Mac小白——如何查看网速
  7. 2013 年 2 月专访董明珠
  8. [ZZ]Sign Up for the First-Ever Appium Roadshow on August 20th in New York City
  9. 20年,中国互联网主流产品的演变和逻辑
  10. 随手练——小米OJ 高弗雷勋爵