LeetCode-1114. 按序打印(多线程)

2024-06-03 13:32

本文主要是介绍LeetCode-1114. 按序打印(多线程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LeetCode 题目描述

我们提供了一个类:

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"。

示例 2:

输入: [1,3,2]
输出: "onetwothree"
解释: 
输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
正确的输出是 "onetwothree"。

注意:

尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。

你看到的输入格式主要是为了确保测试的全面性。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-in-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

解题思路为:

  • 多线程并发访问同一个对象的成员方法
  • 成员方法的执行顺序是固定的(突破点)

对于多线程固定顺序的执行,我们可以采用 Thread.join 方法。但此处不适用,此处我们只可以修改方法内部实现机制,并不能控制线程执行的方式,所以应该是线程等待唤醒机制。

顺序执行实现方案:

  • 方法内部使用布尔变量控制当前方法是否可以执行,即 while(boolean) 范式,某个方法执行后修改布尔值达到顺序控制(while 循环导致性能考虑)
  • 使用并发工具类 Semaphore 实现(初始许可为 0,执行前置条件后释放一个许可)
  • 使用并发工具类 CountDownLatch 实现(初始计数器为 1,执行前置条件后计数减 1)

1.Semaphore 实现

class Foo {private final CountDownLatch secondLatch = new CountDownLatch(1);private final CountDownLatch thirdLatch = 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();secondLatch.countDown(); // 放行 second 方法}public void second(Runnable printSecond) throws InterruptedException {secondLatch.await();// printSecond.run() outputs "second". Do not change or remove this line.printSecond.run();thirdLatch.countDown(); // 放行 third 方法}public void third(Runnable printThird) throws InterruptedException {thirdLatch.await();// printThird.run() outputs "third". Do not change or remove this line.printThird.run();}
}

2.Semaphore 实现

class Foo {private final Semaphore secondLatch = new Semaphore(0);private final Semaphore thirdLatch = new Semaphore(0);public Foo() {}public void first(Runnable printFirst) throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.printFirst.run();secondLatch.release(); // 放行 second 方法}public void second(Runnable printSecond) throws InterruptedException {secondLatch.acquire();// printSecond.run() outputs "second". Do not change or remove this line.printSecond.run();thirdLatch.release(); // 放行 third 方法}public void third(Runnable printThird) throws InterruptedException {thirdLatch.acquire();// printThird.run() outputs "third". Do not change or remove this line.printThird.run();}
}

参考

有关并发编程相关知识参考本博客专栏对应文章。

这篇关于LeetCode-1114. 按序打印(多线程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1027123

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

多线程解析报表

假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。 Way1 join import java.time.LocalTime;public class Main {public static void main(String[] args) thro

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

Java 多线程概述

多线程技术概述   1.线程与进程 进程:内存中运行的应用程序,每个进程都拥有一个独立的内存空间。线程:是进程中的一个执行路径,共享一个内存空间,线程之间可以自由切换、并发执行,一个进程最少有一个线程,线程实际数是在进程基础之上的进一步划分,一个进程启动之后,进程之中的若干执行路径又可以划分成若干个线程 2.线程的调度 分时调度:所有线程轮流使用CPU的使用权,平均分配时间抢占式调度

Java 多线程的基本方式

Java 多线程的基本方式 基础实现两种方式: 通过实现Callable 接口方式(可得到返回值):