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】的更多相关文章

  1. LeetCode 按序打印

    第1114题 我们提供了一个类: public class Foo {   public void one() { print("one"); }   public void tw ...

  2. ERP按序打印问题

    按序打印只适合一个机器,不适合主副机模式,主副机模式请勾选同时打印 如果开启主副机模式勾选了按序打印,会造成副机下厨后厨不出单

  3. LeetCode:打印零与奇偶数【1116】

    LeetCode:打印零与奇偶数[1116] 题目描述 假设有这么一个类: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 构造函数 ...

  4. Java多线程wait和notify协作,按序打印abc

    有一个经典的多线程面试题:启三个线程,按序打印ABC 上代码: package cn.javaBase.study_thread1; class MyRunnable1 implements Runn ...

  5. LeetCode 从头到尾打印链表

    LeetCode 从头到尾打印链表 题目描述 输入一个链表头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 一得之见(Jav ...

  6. (LeetCode)1114. 按序打印

    题目来源:https://leetcode-cn.com/problems/print-in-order/ 我们提供了一个类: public class Foo {  public void one( ...

  7. [LeetCode]1114. 按序打印(并发)

    ####题目 我们提供了一个类: public class Foo {   public void one() { print("one"); }   public void tw ...

  8. LeetCode:交替打印【1115】

    LeetCode:交替打印[1115] 题目描述 我们提供一个类: class FooBar { public void foo() { for (int i = 0; i < n; i++) ...

  9. LeetCode 题解目录

    前言 本目录将不断更新记录leetcode的刷题日记. 二叉树 序号 标题 难度 标签 1 108 将有序数组转换为二叉搜索树 简单 树.深度优先搜索 2 538 把二叉搜索树转换为累加树 简单 树 ...

随机推荐

  1. NISP二级笔记(二) 信息安全管理体系

  2. HiveQL Index 索引

    Hive只有有限的索引功能.Hive中没有普通关系型数据库中键的概念,但是还是可以对一些字段建立索引来加速某些操作.一张表的索引数据存储在另外一张表中. 通过explain命令可以查看某个查询语句是否 ...

  3. 自用java购物

    @RequestMapping("listgoodscart") public ResultEntity listGoodsCart(@RequestParam(name = &q ...

  4. zabbix sender

    在zabbix中自定义一个虚拟主机,自定义key值,一般运用的是自动发现规则,给清单规则中配置上宏变量,通过py脚本调动zabbixsender模块,给这个主机,host发送一组包含键和宏变量的值,这 ...

  5. HTML5新增常用标签

    1.header 标签定义文档的页眉(介绍信息). <body> <article> <header> <h1>What Does WWF Do?< ...

  6. Python学习之--用户输入以及运算

    一. 用户输入:input 1. 函数input() 让程序暂停运行,等待用户输入一些文本. 2.使用int()将字符串转为数值 二.运算:+-*. 1.求模运算符 (%)将两个数相除并返回余数: 2 ...

  7. 99: AGC-018C 堆+思维

    $des$有 $X + Y + Z$ 个人,第 $i$ 个人有 $A_i$ 个金币,$B_i$ 个银币,$C_i$ 个铜币.选出 $X$ 个人获得其金币,选出 $Y$ 个人获得其银币,选出 $Z$ 个 ...

  8. ZOJ 1608 Two Circles and a Rectangle

    Give you two circles and a rectangle, your task is to judge wheather the two circles can be put into ...

  9. 38、数据源Parquet之使用编程方式加载数据

    一.概述 Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,最新的版本是1.8.0. 列式存 ...

  10. 一次 react-router 中遇到的小坑

    react-router Link 标签不生效的问题 废话不多说, 直接上问题, 排解过程和答案 现象: 发现 使用 Link 标签没有 元素的样式和效果, 也不能进行跳转 代码如下: render( ...