[Java]LeetCode1114. 按序打印 | Print in Order
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址://www.cnblogs.com/strengthen/p/11289226.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Suppose we have a class:
public class Foo {
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}
The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().
Example 1:
Input: [1,2,3]
Output: "firstsecondthird"
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.
Example 2:
Input: [1,3,2]
Output: "firstsecondthird"
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). "firstsecondthird" is the correct output.
Note:
We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.
我们提供了一个类:
public class Foo {
public void one() { print("one"); }
public void two() { print("two"); }
public void three() { print("three"); }
}
三个不同的线程将会共用一个 Foo 实例。
线程 A 将会调用 one() 方法
线程 B 将会调用 two() 方法
线程 C 将会调用 three() 方法
请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。
示例 1:
输入: [1,2,3]
输出: "onetwothree"
解释:
有三个线程会被异步启动。
输入 [1,2,3] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 two() 方法,线程 C 将会调用 three() 方法。
正确的输出是 "onetwothree"。
示例 2:
输入: [1,3,2]
输出: "onetwothree"
解释:
输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
正确的输出是 "onetwothree"。
注意:
尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。
你看到的输入格式主要是为了确保测试的全面性。
10ms
1 import java.util.concurrent.Semaphore; 2 3 class Foo { 4 Semaphore A; 5 Semaphore B; 6 Semaphore C; 7 8 public Foo() { 9 A = new Semaphore(1); 10 B = new Semaphore(0); 11 C = new Semaphore(0); 12 } 13 14 public void first(Runnable printFirst) throws InterruptedException { 15 A.acquire(); 16 // printFirst.run() outputs "first". Do not change or remove this line. 17 printFirst.run(); 18 B.release(); 19 } 20 21 public void second(Runnable printSecond) throws InterruptedException { 22 B.acquire(); 23 // printSecond.run() outputs "second". Do not change or remove this line. 24 printSecond.run(); 25 C.release(); 26 } 27 28 public void third(Runnable printThird) throws InterruptedException { 29 C.acquire(); 30 // printThird.run() outputs "third". Do not change or remove this line. 31 printThird.run(); 32 } 33 }
13ms
1 class Foo { 2 3 private boolean firstFinished; 4 private boolean secondFinished; 5 private Object lock = new Object(); 6 7 public Foo() { 8 9 } 10 11 public void first(Runnable printFirst) throws InterruptedException { 12 13 synchronized (lock) { 14 // printFirst.run() outputs "first". Do not change or remove this line. 15 printFirst.run(); 16 firstFinished = true; 17 lock.notifyAll(); 18 } 19 } 20 21 public void second(Runnable printSecond) throws InterruptedException { 22 23 synchronized (lock) { 24 while (!firstFinished) { 25 lock.wait(); 26 } 27 28 // printSecond.run() outputs "second". Do not change or remove this line. 29 printSecond.run(); 30 secondFinished = true; 31 lock.notifyAll(); 32 } 33 } 34 35 public void third(Runnable printThird) throws InterruptedException { 36 37 synchronized (lock) { 38 while (!secondFinished) { 39 lock.wait(); 40 } 41 42 // printThird.run() outputs "third". Do not change or remove this line. 43 printThird.run(); 44 } 45 } 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了