当前位置:网站首页>LeetCode1114. 按序打印
LeetCode1114. 按序打印
2022-06-28 21:00:00 【Yuyy】
本文最后更新于 481 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
多个线程顺序执行,方法有很多,例如CountDownLatch,wait,volatile,join,semaphore,Automatic。。。
二、问题
我们提供了一个类:
public class Foo {
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}三个不同的线程 A、B、C 将会共用一个 Foo 实例。
- 一个将会调用
first()方法 - 一个将会调用
second()方法 - 还有一个将会调用
third()方法
请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。
示例 1:
输入: [1,2,3]
输出: "firstsecondthird"
解释:
有三个线程会被异步启动。
输入 [1,2,3] 表示线程 A 将会调用 first() 方法,线程 B 将会调用 second() 方法,线程 C 将会调用 third() 方法。
正确的输出是 "firstsecondthird"。示例 2:
输入: [1,3,2]
输出: "firstsecondthird"
解释:
输入 [1,3,2] 表示线程 A 将会调用 first() 方法,线程 B 将会调用 third() 方法,线程 C 将会调用 second() 方法。
正确的输出是 "firstsecondthird"。提示:
- 尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。
- 你看到的输入格式主要是为了确保测试的全面性。
- 244
- 0
三、代码
class Foo {
private CountDownLatch c1=new CountDownLatch(1);
private CountDownLatch c2=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();
c1.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
c1.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
c2.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
c2.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}Post Views: 406
边栏推荐
- [Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
- Employee salary management system
- Automatic operation and maintenance platform based on Apache APIs
- 题解 Ananagrams(UVa156)紫书P113map的应用
- ANR问题--相机相关的debug
- 接口测试流程
- LeetCode560. 和为K的子数组
- [learning notes] cluster analysis
- Application of Andy s first dictionary (uva10815) Purple Book p112set
- Flask - Summary
猜你喜欢

How to analyze the relationship between enterprise digital transformation and data asset management?

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application

The principle and source code analysis of Lucene index construction

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

with torch.no_grad():的使用原因

什么是接口?什么是接口测试?

Bitbucket 使用 SSH 拉取仓库失败的问题

基于 Apache APISIX 的自动化运维平台

CNN-LSTM的flatten
随机推荐
LeetCode117. 填充每个节点的下一个右侧节点指针_II
视频号如何下载视频?来看超简单方法!
Résumé de la stabilité
openGauss内核分析之查询重写
MongoDB——副本集与分片
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
How to recover after Oracle delete accidentally deletes table data
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Bitbucket failed to pull the warehouse Using SSH
ANR无响应介绍
2. integrate filter
[try to hack] cobalt strike (I)
How do I download videos? Look at the super simple method!
Analysis of variance
No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
Web 自动化环境搭建
学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
大智慧上怎么进行开户啊, 安全吗
How to analyze the relationship between enterprise digital transformation and data asset management?
LeetCode每日一题——剑指 Offer II 091. 粉刷房子