Leetcode 1144
今天做了一个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() 方法之后被执行。
这个题目的关键一目了然,就是如何保证三个线程按照指定的顺序去依次执行,即先执行A,然后B,最后C。
方法一
类似于cas自旋:
class Foo {private volatile int a = 1;public Foo() {}public void first(Runnable printFirst) throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.for(;;){if(a==1){printFirst.run();a = 2;break;}} }public void second(Runnable printSecond) throws InterruptedException {// printSecond.run() outputs "second". Do not change or remove this line.for(;;){if(a==2){printSecond.run();a = 3;break;}} }public void third(Runnable printThird) throws InterruptedException {// printThird.run() outputs "third". Do not change or remove this line.for(;;){if(a==3){printThird.run();a = 1;break;}} }
}
方法二
使用关键字synchronized以及方法wait()和notify(),相当于设置了两道屏障:
class Foo {boolean first_finished = false;boolean second_finished = false;Object lock = new Object();public Foo() {}public void first(Runnable printFirst) throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.synchronized(lock){printFirst.run();first_finished = true;lock.notifyAll();}}public void second(Runnable printSecond) throws InterruptedException {// printSecond.run() outputs "second". Do not change or remove this line.synchronized(lock){while(!first_finished){lock.wait();}printSecond.run();second_finished = true;lock.notifyAll();}}public void third(Runnable printThird) throws InterruptedException {// printThird.run() outputs "third". Do not change or remove this line.synchronized(lock){while(!second_finished){lock.wait();}printThird.run();lock.notifyAll();}}
}
方法三
使用CountDownLatch这个类,用来计数需要等待线程的数量。
class Foo {private CountDownLatch cnt_A = new CountDownLatch(1);private CountDownLatch cnt_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();cnt_A.countDown();}public void second(Runnable printSecond) throws InterruptedException {// printSecond.run() outputs "second". Do not change or remove this line.cnt_A.await();printSecond.run();cnt_B.countDown();}public void third(Runnable printThird) throws InterruptedException {// printThird.run() outputs "third". Do not change or remove this line.cnt_B.await();printThird.run();}
}
Leetcode 1144相关推荐
- LeetCode 1144. 递减元素使数组呈锯齿状(奇偶分别遍历)
1. 题目 给你一个整数数组 nums,每次 操作 会从中选择一个元素并 将该元素的值减少 1. 如果符合下列情况之一,则数组 A 就是 锯齿数组: 每个偶数索引对应的元素都大于相邻的元素,即 A[0 ...
- LeetCode 1144.递减元素使数组呈锯齿状: 贪心
直接从头到尾遍历,判断当前元素索引奇偶,如果当前是偶数便认为是在对"奇数最大"情况进行计算,将当前数字比两边数字多出来的地方(两侧差值取最大加1)累加到res_odd中,同理当前是 ...
- 【leetcode】最强边界条件
数组的边界 1144. 递减元素使数组呈锯齿状 给你一个整数数组 nums,每次 操作 会从中选择一个元素并 将该元素的值减少 1. 如果符合下列情况之一,则数组 A 就是 锯齿数组: 每个偶数索引对 ...
- Java算法:LeetCode算法Java版合集1111-1588题
1111. 有效括号的嵌套深度 题目描述 有效括号字符串 仅由 "(" 和 ")" 构成,并符合下述几个条件之一: 空字符串 连接,可以记作 AB(A 与 B ...
- [LeetCode]135.Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode】(55)Jump Game(Medium)
题目 Jump Game Total Accepted: 52275 Total Submissions: 194395My Submissions Question Solution Given ...
- Leetcode每日一题总目录(动态更新。。。)
0. 概要 leecode每日一题(也可能多题)题解跟踪记录及总目录. 常用算法解题思路和技巧及数据结构: 预处理:数组排序(954),哈希表... 双指针法 682,125,905 单向链表 2 双 ...
- LeetCode之Candy
[题目] There areNchildren standing in a line. Each child is assigned a rating value. You are giving ca ...
- leetcode 5. Longest Palindromic Substring 字符串中的最长回文数 逐步从O(n^2)优化至线性时间
题目 解析 思路一 暴力解法 思路二 指针+最大长度 思路3 由中间至两边找回数 思路4 Manacher's algorithm 线性时间 参考文档 题目 链接 给定一个字符串 s,找到 s 中最长 ...
最新文章
- HP 服务器 iLO 远程控制软件 介绍
- 隧道技术_隧道施工关于新防水工艺技术
- 2021年5月信息系统项目管理师案例分析真题+视频讲解(3)
- Apple Swift 编程语言入门教程
- VS Code 设置好看的字体:Operator Mono
- 最新版飞鸽传书(http://www.freeeim.com)下载
- 简单python脚本实例-你用 Python 写过哪些有趣的脚本?
- .net framework3.5新特性1:Lambda表达式
- 一个开源vue网站博客,nuxt开源网站,前后端分离项目
- PAT A1007 动态规划
- 谁会成为中国互联网下一代英雄
- SpringMVC_开天辟地
- 算术平方根计算机保留根号,根号计算器
- 高斯 matlab程序,一个计算高斯积分点坐标和权的MATLAB程序
- 思科路由器系统是Linux,思科推基于Linux新款无线存储路由器
- 数据源EPMSSqlDataSource的使用
- 【私藏】开发APP必须知道的API集合
- PyTorch中tensor介绍
- 基于python的管理系统_基于ssm的管理系统_基于python管理系统
- 1、Python学习笔记第一课:python介绍