LeetCode:按序打印【1114】
LeetCode:按序打印【1114】
题目描述
我们提供了一个类:
|
1
2
3
4
5
|
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"。
题目分析
two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行,即two、three的前置任务(线程)均有1个,我们可以引入倒计时器,并初始化其值为1。
这样,每当one完成后,two被唤醒,每当two完成后,three被唤醒。
Java题解
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import java.util.concurrent.CountDownLatch;class Foo { private CountDownLatch count_a = new CountDownLatch(1); private CountDownLatch count_b = 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(); count_a.countDown(); } public void second(Runnable printSecond) throws InterruptedException { count_a.await(); // printSecond.run() outputs "second". Do not change or remove this line. printSecond.run(); count_b.countDown(); } public void third(Runnable printThird) throws InterruptedException { count_b.await(); // printThird.run() outputs "third". Do not change or remove this line. printThird.run(); }} |
LeetCode:按序打印【1114】的更多相关文章
- LeetCode 按序打印
第1114题 我们提供了一个类: public class Foo { public void one() { print("one"); } public void tw ...
- ERP按序打印问题
按序打印只适合一个机器,不适合主副机模式,主副机模式请勾选同时打印 如果开启主副机模式勾选了按序打印,会造成副机下厨后厨不出单
- LeetCode:打印零与奇偶数【1116】
LeetCode:打印零与奇偶数[1116] 题目描述 假设有这么一个类: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 构造函数 ...
- Java多线程wait和notify协作,按序打印abc
有一个经典的多线程面试题:启三个线程,按序打印ABC 上代码: package cn.javaBase.study_thread1; class MyRunnable1 implements Runn ...
- LeetCode 从头到尾打印链表
LeetCode 从头到尾打印链表 题目描述 输入一个链表头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 一得之见(Jav ...
- (LeetCode)1114. 按序打印
题目来源:https://leetcode-cn.com/problems/print-in-order/ 我们提供了一个类: public class Foo { public void one( ...
- [LeetCode]1114. 按序打印(并发)
####题目 我们提供了一个类: public class Foo { public void one() { print("one"); } public void tw ...
- LeetCode:交替打印【1115】
LeetCode:交替打印[1115] 题目描述 我们提供一个类: class FooBar { public void foo() { for (int i = 0; i < n; i++) ...
- LeetCode 题解目录
前言 本目录将不断更新记录leetcode的刷题日记. 二叉树 序号 标题 难度 标签 1 108 将有序数组转换为二叉搜索树 简单 树.深度优先搜索 2 538 把二叉搜索树转换为累加树 简单 树 ...
随机推荐
- Apache Solr Velocity模板远程代码执行
更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. 这里用的docker环境 很简单的 在这里不再介绍 本地搭建好环境然后访问8983端口 网页如下: 查下节点名称 同样名字可以访问http ...
- Linux 一条长命令占用多行
前言 考察下面的脚本: ? 1 emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --so ...
- learning java 正则表达式
var regStr = "Java is very good"; Matcher m = Pattern.compile("\\w+").matcher(re ...
- 【dp】P1541 乌龟棋
链接: https://www.luogu.org/problemnew/show/P1541 [思路]: 用f[a][b][c][d]表示,第一张卡用a张,第二张卡用b张..........然后就盘 ...
- 影像优化 OptimizeRaster工具包介绍
Esri OptimizeRasters是一个高效.可配置的开源工具包. OptimizeRasters提供了以下功能: 影像格式转换和压缩.支持输出优化栅格格式:MRF.分块TIFF.云存储优化Ge ...
- Lua chunk文件结构
1.lua执行经过: xx.lua源码文件------->执行(lua虚拟机) 隐式调用luac编译器 我们可以直接用luac命令去编译lua源码文件,然后用编译后的文件运行在lvm(lua虚拟 ...
- 1.xml解析
public static void main(String[] args) throws DocumentException { SAXReader saxReader = new ...
- 解决tecplot中壁面速度不为0的问题
当直接将fluent的.cas文件和.dat文件导入tecplot中进行后处理的时候,我们会发现,壁面速度不为0的情况(见上图). 出现这样问题的原因为:fluent的计算数据是存储在每个单元的中心位 ...
- 利用ssh 删除远程服务器文件
ssh 登录,利用命令 rm 登录ssh ssh name@10.202.9.11 rm 命令删除文件 rm file rm -rf 删除文件夹 rm -rf fold 删除当前文件夹内容,保留当前文 ...
- Thingsboard学习之三启动Thingsboard
关于安装Docker和Docker-Compose,参考<Thingsboard学习之二安装Docker和Docker-Compose> 首先检查一下是否有安装git yum instal ...