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. sqoop job 实现自动增量导入

    一.测试环境 1.MySQL表结构 mysql> show create table autoextend\GCREATE TABLE `autoextend` (  `id` bigint(2 ...

  2. Intel 80386 CPU

    一.80386 概述 80386处理器被广泛应用在1980年代中期到1990年代中期的IBM PC相容机中.这些PC机称为「80386电脑」或「386电脑」,有时也简称「80386」或「386」.80 ...

  3. Dominating Patterns (AC 自动鸡模版题, 出现次数最多的子串)

    传送门 题意: 给你n个模式串, 再给你一个 文本串,问模式串在文本串中出现次数最多是多少. 出现次数最多的模式串有哪些. 解: 模版题. #include <bits/stdc++.h> ...

  4. 百度UEditor富文本插件的使用

    这个富文本还是功能挺全的. 官方文档地址 下载地址 常用接口 较完整代码仓库 UEditor下载后直接运行即可访问,但在上传文件时需要单独再做配置. [很详细的SpringBoot整合UEditor教 ...

  5. FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,

    在对nginx添加fastCGI的支持后,然后进行php页面验证,发现页面识别不到,不清楚什么情况,随后google了下,原来是Nginx内置变量问题惹的祸. 1.平台介绍: 1 2 3 4 5 6 ...

  6. AtCoder Grand Contest 016题解

    传送门 \(A\) 直接枚举最终的字母然后模拟就行了-- 就这数据范围还是别学我写的这种做法了-- const int N=105; char s[N];int las[26],mx[26],n,re ...

  7. linux 查看内存,free,ps,说明Buffers,Cached,SReclaimable

    查看机器剩余内存free即可,百度就可以轻松查到,主要想说的 查所有进程占用内存情况并排序: ps aux | sort -nk5 k5代表根据RSS排序,k6代表VSZ排序. ----------- ...

  8. P5385 [Cnoi2019]须臾幻境(LCT+主席树,思维题)

    题目 P5385 [Cnoi2019]须臾幻境 做法 考虑一条边\((u,v)\)是否\([L,R]\)中的贡献:\([L,R]\)中第一条位于\(u,v\)链的边,则减少了一个联通块 实现:\(LC ...

  9. C语言的柔性数组的实现及应用

    c编程的时候数组长度一般都是固定好的,实际上c还能实现变长数组.其实c99中确实是有变长数组的说法,C99中通过允许结构体中的最后一个成员是长度未知的数组实现变长数组,其定义格式如下: typedef ...

  10. select2多选设置select多选,select2取值和赋值

    select2设置select多选,select2取值和赋值,作为筛选条件的时候,取值相对简单,把选中的id值转为字符串传给后端查询,查询之后会刷新页面,为了在下拉框中显示刚刚选中的值,就需要给sel ...