当前位置: 首页 > news >正文

交替输出

交替输出

题目:线程 1 输出 a 5 次,线程 2 输出 b 5 次,线程 3 输出 c 5 次。现在要求输出 abcabcabcabcabc

wait notify 版

public class SyncWaitNotify {private volatile int flag;private volatile int loopNumber;public SyncWaitNotify(int flag,int loopNumber){this.flag = flag;this.loopNumber = loopNumber;}public void print(int flag,int next,String str){for(int i=0;i<loopNumber;i++){synchronized (this){while (this.flag != flag){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.print(str);this.flag = next;this.notifyAll();}}}public static void main(String[] args) {SyncWaitNotify syncWaitNotify = new SyncWaitNotify(1,5);new Thread(() -> {syncWaitNotify.print(1,2,"a");}).start();new Thread(() -> {syncWaitNotify.print(2,3,"b");}).start();new Thread(() -> {syncWaitNotify.print(3,1,"c");}).start();}
}

Lock 条件变量版

public class AwaitSignal extends ReentrantLock {private volatile int loopNumber;public AwaitSignal(int loopNumber) {this.loopNumber = loopNumber;}public void print(Condition condition,Condition next,String str){for(int i=0;i<loopNumber;i++){this.lock();try {try {condition.await();System.out.print(str);next.signal();} catch (InterruptedException e) {e.printStackTrace();}} finally {this.unlock();}}}public void start(Condition condition){this.lock();try {condition.signal();} finally {this.unlock();}}public static void main(String[] args) {AwaitSignal awaitSignal = new AwaitSignal(5);Condition aWait = awaitSignal.newCondition();Condition bWait = awaitSignal.newCondition();Condition cWait = awaitSignal.newCondition();new Thread(() -> {awaitSignal.print(aWait,bWait,"a");}).start();new Thread(() -> {awaitSignal.print(bWait,cWait,"b");}).start();new Thread(() -> {awaitSignal.print(cWait,aWait,"c");}).start();awaitSignal.start(aWait);}
}

Park Unpark 版

public class SyncPark {private int loopNumber;public Thread[] threads;public SyncPark(int loopNumber){this.loopNumber = loopNumber;}public void print(int next,String str){for(int i=0;i<loopNumber;i++){LockSupport.park();System.out.print(str);LockSupport.unpark(threads[next]);}}public void start(Thread thread){LockSupport.unpark(thread);}public static void main(String[] args) {SyncPark syncPark = new SyncPark(5);Thread t1 = new Thread(() -> {syncPark.print(1, "a");}, "t1");Thread t2 = new Thread(() -> {syncPark.print(2, "b");}, "t2");Thread t3 = new Thread(() -> {syncPark.print(0, "c");}, "t3");syncPark.threads = new Thread[]{t1,t2,t3};t1.start();t2.start();t3.start();syncPark.start(t1);}
}
http://www.lryc.cn/news/426992.html

相关文章:

  • JS(三)——更改html内数据
  • CSS小玩意儿:文字适配背景
  • C++:平衡二叉搜索树之红黑树
  • CentOS 7 系统优化
  • 扫雷游戏——附源代码
  • Vue3列表(List)
  • HarmonyOS NEXT - Navigation组件封装BaseNavigation
  • 浅看MySQL数据库
  • Pytorch常用训练套路框架(CPU)
  • C++ | Leetcode C++题解之第338题比特位计数
  • 智慧校园云平台电子班牌系统源码,智慧教育一体化云解决方案
  • 数据库系统 第17节 数据仓库 案例赏析
  • 硬件面试经典 100 题(71~90 题)
  • 【git】代理相关
  • golang gin框架中创建自定义中间件的2种方式总结 - func(*gin.Context)方式和闭包函数方式定义gin中间件
  • Linux高级编程 8.13 文件IO
  • 【k8s】ubuntu18.04 containerd 手动从1.7.15 换为1.7.20
  • 常用浮动方式
  • 设计模式反模式:UML常见误用案例分析
  • Python编码系列—Python SQL与NoSQL数据库交互:深入探索与实战应用
  • 贪心算法---跳跃游戏
  • 利用EditPlus进行Json数据格式化
  • xss.function靶场(easy)
  • 【LLM入门】Let‘s reproduce GPT-2 (124M)【完结,重新回顾一下,伟大!】
  • c语言----取反用什么符号
  • 【html+css 绚丽Loading】 - 000003 乾坤阴阳轮
  • 【Web】巅峰极客2024 部分题解
  • 在AMD GPU上进行Grok-1模型的推理
  • 在亚马逊云科技上部署开源大模型并利用RAG和LangChain开发生成式AI应用
  • Spring——Bean的生命周期