[LeetCode]1114. 按序打印(并发)
####题目
我们提供了一个类:
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() 方法之后被执行。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-in-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
####题解
使用synchronzed锁+while(条件)的方式,使用线程的等待通知实现。
second方法等待firstFinished;third方法等待secondFinished。
####代码
class Foo {
private Object obj = new Object();
private boolean firstFinished = false;
private boolean secondFinished = false;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
synchronized (obj){
printFirst.run();
firstFinished = true;
obj.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
// printSecond.run() outputs "second". Do not change or remove this line.
synchronized(obj){
while(!firstFinished){
obj.wait();
}
printSecond.run();
secondFinished = true;
obj.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
// printThird.run() outputs "third". Do not change or remove this line.
synchronized(obj){
while(!secondFinished){
obj.wait();
}
printThird.run();
}
}
}
[LeetCode]1114. 按序打印(并发)的更多相关文章
- (LeetCode)1114. 按序打印
题目来源:https://leetcode-cn.com/problems/print-in-order/ 我们提供了一个类: public class Foo { public void one( ...
- LeetCode:按序打印【1114】
LeetCode:按序打印[1114] 题目描述 我们提供了一个类: 1 2 3 4 5 public class Foo { public void one() { print("on ...
- LeetCode:交替打印【1115】
LeetCode:交替打印[1115] 题目描述 我们提供一个类: class FooBar { public void foo() { for (int i = 0; i < n; i++) ...
- ERP按序打印问题
按序打印只适合一个机器,不适合主副机模式,主副机模式请勾选同时打印 如果开启主副机模式勾选了按序打印,会造成副机下厨后厨不出单
- Java多线程wait和notify协作,按序打印abc
有一个经典的多线程面试题:启三个线程,按序打印ABC 上代码: package cn.javaBase.study_thread1; class MyRunnable1 implements Runn ...
- LeetCode 按序打印
第1114题 我们提供了一个类: public class Foo { public void one() { print("one"); } public void tw ...
- 【Leetcode】交替打印FooBar
[问题]我们提供一个类: class FooBar { public void foo() { ; i < n; i++) { print("foo"); } } publi ...
- Java并发相关知识点梳理和研究
1. 知识点思维导图 (图比较大,可以右键在新窗口打开) 2. 经典的wait()/notify()/notifyAll()实现生产者/消费者编程范式深入分析 & synchronized 注 ...
- LeetCode 题解目录
前言 本目录将不断更新记录leetcode的刷题日记. 二叉树 序号 标题 难度 标签 1 108 将有序数组转换为二叉搜索树 简单 树.深度优先搜索 2 538 把二叉搜索树转换为累加树 简单 树 ...
随机推荐
- 【高阶版】Python词典
使用dict.fromkeys()创建词典的一个坑 创建词典有三种方法,第一是直接赋值,d = {1:2, 2:3}:第二个是,通过构造方法,d = dict([(1, 2), (2, 3)]),第三 ...
- 对比两张Excel表数据差异时,遇到数据雷响不一致
表A中为文本(有绿色三角符号),表B为数字(没有三角符号),而自动对比时会检查数据类型,怎么办? 执行对比: 得到结果: 这时候要解决类型问题(即绿色三角形标志) 点击灰色区域全选 哪个黄色感叹号可以 ...
- mysql无法远程连接问题(ERROR 1045 (28000): Access denied for user 'root')
mysql版本 : 8.0.21 使用mysql 作为nextcloud的数据库.之前使用挺正常的,因为被黑客勒索过一次,重新启动了一个mysql的docker镜像. 结果数据库配置老是失败,next ...
- 【原创】Linux虚拟化KVM-Qemu分析(一)
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...
- ssh连接:Socket error Event: 32 Error: 10053.
今天在使用xshell连接刚装的linux系统的时候,发现无法建立连接,会报如下错误: Connecting to 192.168.21x.x:22...Connection established. ...
- Kafka工作流程
Kafka生产过程分析 1 写入方式 producer采用推(push)模式将消息发布到broker,每条消息都被追加(append)到分区(patition)中,属于顺序写磁盘(顺序写磁盘效率比随机 ...
- springsession
Spring Session 一. HttpSession 回顾 1 什么是 HttpSession 是 JavaWeb 服务端提供的用来建立与客户端会话状态的对象. 二. Session 共享 1 ...
- flutter 设置状态栏的背景与颜色
flutter 设置状态栏的背景与颜色 导包 import 'dart:io'; import 'package:flutter/services.dart'; 在main()函数中添加以下函数, v ...
- 终于弄明白了 Singleton,Transient,Scoped 的作用域是如何实现的
一:背景 1. 讲故事 前几天有位朋友让我有时间分析一下 aspnetcore 中为什么向 ServiceCollection 中注入的 Class 可以做到 Singleton,Transient, ...
- ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查
三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...