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 把二叉搜索树转换为累加树 简单 树 ...
随机推荐
- Nginx和php-fpm的启用和停用脚本
#!/bin/bash #停止php-fpm sudo php -v ps -ef | grep php-fpm | sed '$d' echo "..................... ...
- JS发送验证码;并设置cookie
Tool.send_code = function(obj) { var isCheck = true, form = $('#editInfo_Form'); var mobile = form.f ...
- printf的使用
#!/bin/bashprintf "|------------------------------------\n"printf "this is printf str ...
- C 指针常量 和常量指针 指向常量的指针常量的使用
#include <stdio.h> /* 指针常量 和常量指针 指向常量的指针常量 */ int main() { int a = 100; int b =200; int* const ...
- (17)打鸡儿教你Vue.js
vue-router <a class="list-group-item" v-link="{ path: '/home'}">Home</a ...
- 《挑战30天C++入门极限》引言
作为一个长篇的C++入门教程,无论如何也应该有这么个引言,可是文笔并不好的我,想了很久也不知道该如何写...... 仔细想想,与其把这篇短文当作教程的引言,其实它更应该是一篇引导初学者步入C++殿堂的 ...
- 内核中根据进程Pid获取卷的全目录
目录 一丶简介 二丶原理 3.代码实现. 一丶简介 在内核中有时候想通过PID 获取进程的全路径以达到监控的作用 比如我们设置了进程回调.则可以根据PID看下进程的全路径. 二丶原理 原理就是在内核中 ...
- mapper.xml等资源导入的问题
在pom.xml中的下导入如下字段 <resources> <resource> <directory>src/main/java</directory> ...
- 2019.10.25字符串——zr
题意: 给你两个字符串,由01组成:求他们两个的最短公共非子序列,要求字典序最小: 非公共子序列:都不是这两个字符串的子序列: 本人只会暴力啊,二进制枚举稳拿15分: 然而这道题其实是一个最短路题: ...
- Pytest权威教程24-Pytest导入机制及系统路径
目录 Pytest导入机制和sys.path/PYTHONPATH 包中的测试脚本及conftest.py文件 独立测试模块及conftest.py文件 调用通过python -m pytest调用p ...