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. leaning java swing 为组件设置边框

    import javax.swing.*; import javax.swing.border.BevelBorder; import javax.swing.border.Border; impor ...

  2. W3C统一验证工具和PR值

    W3C统一验证工具(http://validator.w3.org/)用于分析代码. PR值全称为PageRank(网页级别),PR值是Google用于标识网页的等级.重要性.网站的好坏的重要标准之一 ...

  3. 洛谷 P1613 跑路 题解

    P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...

  4. pcl-qt使用QVTKWidget 与PCLVisualizer 显示雷达点云

    #ifndef PCLVIEWER_H #define PCLVIEWER_H #include "defines.h" #include <iostream> #in ...

  5. 10、spark高级编程

    一.基于排序机制的wordcount程序 1.要求 1.对文本文件内的每个单词都统计出其出现的次数. 2.按照每个单词出现次数的数量,降序排序. 2.代码实现 ------java实现------- ...

  6. Transform详解(超详细) Attention is all you need论文

    一.背景 自从Attention机制在提出 之后,加入Attention的Seq2 Seq模型在各个任务上都有了提升,所以现在的seq2seq模型指的都是结合rnn和attention的模型.传统的基 ...

  7. os 模块常用方法

    os.remove()删除文件 os.rename()重命名文件 os.walk()生成目录树下的所有文件名 os.chdir()改变目录 os.mkdir/makedirs创建目录/多层目录 os. ...

  8. Node.js之文件下载

    Node.js之文件下载,主要最近解决我的一个需求. 需求描述:如何将腾讯云上传的文件存储到本地某个目录下,如果用js来实现,纯JavaScript没有这样的功能(也许有),正好我这个项目用node. ...

  9. 从零开始带你成为JVM实战高手

    专栏大纲 1.核心gc 内存回收以及提前设置内存大小.

  10. 【微信小程序】scroll-view 的上拉加载和下拉刷新

    1.在微信小程序中,想到 下拉刷新 和 上拉加载,如果是整个页面都拖动的话,可以在页面配置中,配置 enablePullDownRefresh 和 onReachBottomDistance 然后在 ...