news 2025/12/14 10:41:12

并发编程场景题学习

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
并发编程场景题学习

预备知识、各种同步工具

  • synchronized + wait/notify
    概括:Java内置的最基础的线程同步机制,基于对象监视器实现。
    用途:用于简单的线程互斥和等待通知机制,如传统的生产者-消费者问题。

  • CountDownLatch
    概括:一次性的事件等待器,通过倒计时控制线程执行顺序。
    用途:等待多个并行任务全部完成后再继续执行,如初始化完成后启动主应用。

  • CompletableFuture
    概括:声明式的异步编程框架,支持任务链式编排和组合。
    用途:优雅地编排多个异步任务,处理复杂的异步依赖关系,如并行调用多个微服务。

  • Semaphore
    概括:基于许可证的资源控制器,限制并发访问数量。
    用途:控制对有限资源的并发访问,如数据库连接池、限流器。

  • ReentrantLock + Condition
    概括:功能更丰富的显式锁,提供比synchronized更灵活的控制。
    用途:需要高级同步特性的场景,如可中断锁、公平锁、多个等待条件。

  • BlockingQueue
    BlockingQueue 是 Java 并发包中开箱即用的 “生产者 - 消费者” 解决方案,本质是对 ReentrantLock + Condition 的高级封装(比如 ArrayBlockingQueue 底层就是用这组工具实现的)

一、按序打印(Leetcode 1114)

给你一个类:

publicclassFoo{publicvoidfirst(){print("first");}publicvoidsecond(){print("second");}publicvoidthird(){print("third");}}

三个不同的线程 A、B、C 将会共用一个 Foo 实例。

线程 A 将会调用 first() 方法
线程 B 将会调用 second() 方法
线程 C 将会调用 third() 方法
请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。

classFoo{publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{printFirst.run();}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{printSecond.run();}publicvoidthird(RunnableprintThird)throwsInterruptedException{printThird.run();}}

方法一:忙等待
优点:简单
缺点:空转CPU,可能会死循环

classFoo{privatevolatileintx=0;publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{printFirst.run();x++;}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{while(x!=1){}printSecond.run();x++;}publicvoidthird(RunnableprintThird)throwsInterruptedException{while(x!=2){}printThird.run();}}

1)这里x++因为不是原子性操作,如果有多个线程同时调用first/second/third 会出现问题,
2)while循环里面可以加Thread.yield() 减少自旋的 CPU 消耗

classFoo{privateAtomicIntegerx=newAtomicInteger(0);publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{printFirst.run();x.incrementAndGet();}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{while(x.get()!=1){Thread.yield();}printSecond.run();x.incrementAndGet();}publicvoidthird(RunnableprintThird)throwsInterruptedException{while(x.get()!=2){Thread.yield();}printThird.run();}}

方法二:synchronized + wait/notify

classFoo{privateintx=0;privatefinalObjectlock=newObject();publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{synchronized(lock){printFirst.run();x++;lock.notifyAll();}}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{synchronized(lock){while(x!=1){lock.wait();}printSecond.run();x++;lock.notifyAll();}}publicvoidthird(RunnableprintThird)throwsInterruptedException{synchronized(lock){while(x!=2){lock.wait();}printThird.run();lock.notifyAll();}}}

方法三:CountDownLatch

classFoo{privatefinalCountDownLatchlatch1=newCountDownLatch(1);privatefinalCountDownLatchlatch2=newCountDownLatch(1);publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{printFirst.run();latch1.countDown();}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{latch1.await();printSecond.run();latch2.countDown();}publicvoidthird(RunnableprintThird)throwsInterruptedException{latch2.await();printThird.run();}}

方法四:Semaphore

classFoo{privatefinalSemaphoresem2=newSemaphore(0);privatefinalSemaphoresem3=newSemaphore(0);publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{printFirst.run();sem2.release();}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{sem2.acquire();printSecond.run();sem3.release();}publicvoidthird(RunnableprintThird)throwsInterruptedException{sem3.acquire();printThird.run();}}

方法五:ReentrantLock+Condition

classFoo{privatefinalLocklock=newReentrantLock();privatefinalConditioncond1=lock.newCondition();privatefinalConditioncond2=lock.newCondition();privateintflag=0;publicFoo(){}publicvoidfirst(RunnableprintFirst)throwsInterruptedException{lock.lock();try{printFirst.run();flag++;cond1.signal();}finally{lock.unlock();}}publicvoidsecond(RunnableprintSecond)throwsInterruptedException{lock.lock();try{while(flag!=1){cond1.await();}printSecond.run();flag++;cond2.signal();}finally{lock.unlock();}}publicvoidthird(RunnableprintThird)throwsInterruptedException{lock.lock();try{while(flag!=2){cond2.await();}printThird.run();}finally{lock.unlock();}}}

方法六:CompletableFuture

classFoo{privateCompletableFuture<Void>cf1=newCompletableFuture<>();privateCompletableFuture<Void>cf2=newCompletableFuture<>();publicvoidfirst(RunnableprintFirst){printFirst.run();cf1.complete(null);// 信号:first完成了}publicvoidsecond(RunnableprintSecond){cf1.join();// 阻塞等待cf1完成printSecond.run();cf2.complete(null);// 信号:second完成了}publicvoidthird(RunnableprintThird){cf2.join();// 阻塞等待cf2完成printThird.run();}}

二、交替打印(Leetcode 1115)

给你一个类:

classFooBar{publicvoidfoo(){for(inti=0;i<n;i++){print("foo");}}publicvoidbar(){for(inti=0;i<n;i++){print("bar");}}}

两个不同的线程将会共用一个 FooBar 实例:

线程 A 将会调用 foo() 方法,而
线程 B 将会调用 bar() 方法
请设计修改程序,以确保 “foobar” 被输出 n 次。
方法一:忙等待

classFooBar{privateintn;publicFooBar(intn){this.n=n;}privatevolatilebooleanstate=true;publicvoidfoo(RunnableprintFoo)throwsInterruptedException{for(inti=0;i<n;){if(state){printFoo.run();i++;state=false;}else{Thread.yield();}}}publicvoidbar(RunnableprintBar)throwsInterruptedException{for(inti=0;i<n;){if(!state){printBar.run();i++;state=true;}else{Thread.yield();}}}}

方法二:synchronized + wait/notify

classFooBar{privateintn;publicFooBar(intn){this.n=n;}privatebooleanstate=true;privatefinalObjectlock=newObject();publicvoidfoo(RunnableprintFoo)throwsInterruptedException{for(inti=0;i<n;i++){synchronized(lock){while(!state){lock.wait();}printFoo.run();state=false;lock.notifyAll();}}}publicvoidbar(RunnableprintBar)throwsInterruptedException{for(inti=0;i<n;i++){synchronized(lock){while(state){lock.wait();}printBar.run();state=true;lock.notifyAll();}}}}

方法三:Semaphore

classFooBar{privateintn;publicFooBar(intn){this.n=n;}privatefinalSemaphoresem1=newSemaphore(1);privatefinalSemaphoresem2=newSemaphore(0);publicvoidfoo(RunnableprintFoo)throwsInterruptedException{for(inti=0;i<n;i++){sem1.acquire();printFoo.run();sem2.release();}}publicvoidbar(RunnableprintBar)throwsInterruptedException{for(inti=0;i<n;i++){sem2.acquire();printBar.run();sem1.release();}}}

方法四:ReentrantLock+Condition

classFooBar{privateintn;publicFooBar(intn){this.n=n;}privatefinalLocklock=newReentrantLock();privatefinalConditioncond=lock.newCondition();privatebooleanflag=true;publicvoidfoo(RunnableprintFoo)throwsInterruptedException{for(inti=0;i<n;i++){lock.lock();try{while(!flag){cond.await();}printFoo.run();flag=false;cond.signal();}finally{lock.unlock();}}}publicvoidbar(RunnableprintBar)throwsInterruptedException{for(inti=0;i<n;i++){lock.lock();try{while(flag){cond.await();}printBar.run();flag=true;cond.signal();}finally{lock.unlock();}}}}

三、打印零与奇偶数

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/11 16:42:06

【Dify检索排序优化指南】:掌握重排序配置的5大核心技巧

第一章&#xff1a;Dify检索重排序的核心概念与作用在构建基于大语言模型的智能应用时&#xff0c;检索增强生成&#xff08;RAG&#xff09;技术已成为提升回答准确性的关键手段。Dify作为领先的LLMOps平台&#xff0c;在其检索流程中引入了“重排序”&#xff08;Re-ranking&…

作者头像 李华
网站建设 2025/12/11 16:41:56

VideoSrt智能字幕生成工具完整教程

VideoSrt智能字幕生成工具完整教程 【免费下载链接】video-srt-windows 这是一个可以识别视频语音自动生成字幕SRT文件的开源 Windows-GUI 软件工具。 项目地址: https://gitcode.com/gh_mirrors/vi/video-srt-windows 还在为视频字幕制作而烦恼吗&#xff1f;手动输入、…

作者头像 李华
网站建设 2025/12/11 16:41:55

【经验分享】之C++编译报错:undefined reference to

最近用C写程序&#xff0c;同时调用了OpenCV和LibTorch两个库&#xff0c;编译时一直报错 undefined reference to imread&#xff0c;怎么调参数都不行。之前单独用OpenCV都好好的&#xff0c;差点怀疑人生&#xff08;先说好&#xff0c;我C水平仅限于用现成的库&#xff0c;…

作者头像 李华
网站建设 2025/12/11 16:41:45

16、Azure 备份与恢复及混合云配置全解析

Azure 备份与恢复及混合云配置全解析 在当今数字化时代,数据的安全性和可用性至关重要。Azure 提供了一系列强大的工具和服务,如 Azure 备份、Azure Site Recovery(ASR)等,帮助用户保护和迁移数据。同时,混合云解决方案也为企业提供了一种灵活的方式,结合本地基础设施和…

作者头像 李华
网站建设 2025/12/11 16:41:44

17、本地网络与Azure虚拟网络连接全攻略

本地网络与Azure虚拟网络连接全攻略 在当今数字化时代,将本地基础设施与云服务相结合已成为许多企业的必然选择。Azure作为微软提供的强大云计算平台,为企业提供了丰富的资源和服务。本文将详细介绍如何连接本地网络与Azure虚拟网络,以及在混合环境中配置和使用相关服务的方…

作者头像 李华
网站建设 2025/12/11 16:41:26

EdXposed框架完整部署指南:从零开始构建你的Hook王国

EdXposed框架完整部署指南&#xff1a;从零开始构建你的Hook王国 【免费下载链接】EdXposed Elder driver Xposed Framework. 项目地址: https://gitcode.com/gh_mirrors/edx/EdXposed 在Android系统开发领域&#xff0c;EdXposed框架作为功能强大的Hook工具&#xff0c…

作者头像 李华