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 把二叉搜索树转换为累加树 简单 树 ...
随机推荐
- Thinkphp远程代码执行 payload汇总
Thinkphp 5.0.22http://192.168.1.1/thinkphp/public/?s=.|think\config/get&name=database.usernameht ...
- MySQL分区表 非分区索引 无法使用
在<高性能Mysql>这本书的‘如何使用分区’这一小章中,列举的常见问题中,有以下一个问题: 分区列和索引列不匹配 如果定义的索引列和分区列不匹配,会导致查询无法进行分区过滤.假设在列a上 ...
- Java获取类方法上的注解
, fullName.indexOf("$$")); try { clz = Class.forName(fullName); } catch (ClassNotFoundExce ...
- Ubuntu安装php7.0环境
1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...
- 10分钟用Python告诉你两个机器人聊天能聊出什么火花
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 现在不是讲各种各样的人工智能嘛,AI下棋,AI客服,AI玩家--其实我一直很好奇,两个AI碰上会怎样,比如一起下棋,一起打游戏-- 今天做个 ...
- 二扩域(GF(2^m))中的逆矩阵
通常的逆矩阵可以用高斯消去法计算.十分有效.还可以使用LU分解,QR分解等. 二扩域中的逆矩阵则不同.看似简单,其实有别:它的所有元素定义在GF(2^m)中.从理论来看,似乎也可以用高斯消去法,只是计 ...
- shell编程题(二)
计算1-100之和 #!/bin/bash `;do #符号不是单引号 是 1左边的符号 sum=$[$i + $sum ] done echo $sum #!/bin/bash i= n=1 #定义 ...
- Pytest权威教程21-API参考-04-钩子(Hooks)
目录 钩子(Hooks) 引导时的Hook方法 初始化时的Hook方法 测试运行时的Hook方法 收集用例时的Hook方法 生成测试结果时的Hook方法 调试/交互Hook方法 返回: Pytest权 ...
- 「雅礼集训 2017 Day10」拍苍蝇
传送门 Description 有一天,小 A 的母亲对他家里的卫生状况非常不满意,他的房间里有非常多的苍蝇.在母亲的威逼利诱下,小 A 拿起了苍蝇拍去消灭家里的苍蝇.然而,小 A 以前从来没有亲手消 ...
- PHP-FPM远程代码执行漏洞(CVE-2019-11043)
0x00 简介 在长亭科技举办的 Real World CTF 中,国外安全研究员 Andrew Danau 在解决一道 CTF 题目时发现,向目标服务器 URL 发送 %0a 符号时,服务返回异常, ...