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. Oracle 记录下jdbc thin client module名称

    java.util.Properties props = new java.util.Properties(); props.setProperty("password",&quo ...

  2. webuploader+php如何实现分片+断点续传

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  3. am335x system upgrade rootfs using yocto make rootfs(十二)

    1      Scope of Document This document describes how to make am335x arago rootfs using ycoto project ...

  4. synology git 服务器问题处理

    synology git 服务器问题处理 安装 synology 上的 git 套件, 发现使用过程中存在很多问题. permission 问题 ## 将对应的目录设为git所有者 chown git ...

  5. PHP chmod() 函数

    chmod() 函数改变文件模式. 如果成功则返回 TRUE,否则返回 FALSE. 例子 <?php // 所有者可读写,其他人没有任何权限 chmod(); // 所有者可读写,其他人可读 ...

  6. webpack-merge使用说明

    webpack-merge   配置分离 随着我们业务逻辑的增多,图片.字体.css.ES6以及CSS预处理器和后处理器逐渐的加入到我们的项目中来,进而导致配置文件的增多,使得配置文件书写起来比较繁琐 ...

  7. python 的小技巧之统计list里面元素的个数

    一般写法 def count_list(std:list,tongji): i=0 for item in std: if item==tongji: i+=1 print(i) if __name_ ...

  8. requests库详解

    import requests #实例引入 # response = requests.get('http://www.baidu.com') # print(type(response)) # pr ...

  9. linux 命令scp

    scp命令网络传输文件 上传文件 scp 文件名 usename@10.233.23.100:Data/ 上传文件夹到服务器 scp -r 文件夹(不带/)usename@10.233.23.100: ...

  10. Python_Openpyxl 浅谈(最全总结 足够初次使用)

    https://blog.csdn.net/weixin_43094965/article/details/82226263 import datetime from random import ch ...