当前位置: 首页 > news >正文 湖南建设长沙网站建设价格wordpress图片瀑布流 news 2025/10/22 5:12:25 湖南建设长沙网站建设价格,wordpress图片瀑布流,数商云公司简介,成都网站设计服务商文章目录 使用 synchronized 的场景使用 ReentrantLock 的场景综合考虑 使用 synchronized 的场景 synchronized 是 Java 内置的同步机制#xff0c;使用起来比较简单且常用于如下场景#xff1a; 1、简单的同步逻辑#xff1a;当你的同步逻辑非常简单#xff0c;比如只需… 文章目录 使用 synchronized 的场景使用 ReentrantLock 的场景综合考虑 使用 synchronized 的场景 synchronized 是 Java 内置的同步机制使用起来比较简单且常用于如下场景 1、简单的同步逻辑当你的同步逻辑非常简单比如只需要对某个方法或代码块进行互斥访问时synchronized 非常适用因为它的语法简洁且易于维护。 public synchronized void simpleMethod() {// critical section }2、隐式监视器锁synchronized 隐式地使用对象的监视器锁来控制同步不需要显式的锁定和解锁操作因此避免了锁忘记释放的问题。 3、异常安全synchronized 块在异常发生时会自动释放锁确保不会因为未释放锁而导致死锁问题。 使用 ReentrantLock 的场景 ReentrantLock 是 java.util.concurrent.locks 包中提供的更高级的锁机制适用于以下场景 1、需要更灵活的锁控制ReentrantLock 提供了更多的锁控制功能如 tryLock()尝试锁定和 lockInterruptibly()可中断锁定使得能够响应中断或超时。 import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;public class TryLockExample {private final Lock lock new ReentrantLock();public void tryMethod() {if (lock.tryLock()) {try {// critical section} finally {lock.unlock();}} else {// handle the case where lock was not acquired}} }2、需要公平锁机制ReentrantLock 支持公平锁即先请求先获得锁这在某些需要防止线程饥饿的场景中特别有用。 Lock fairLock new ReentrantLock(true); // true for fairness3、需要条件变量ReentrantLock 提供了条件变量Condition可以更精细地控制线程等待和通知机制适用于需要多个条件队列的复杂同步场景。 import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;// 假设我们有一个界面程序其中一个生产者线程和两个消费者线程 // 并且我们希望消费者1只处理奇数编号的任务而消费者2只处理偶数编号的任务。 public class MultiConditionExample {private static final Lock lock new ReentrantLock();private static final Condition conditionOdd lock.newCondition();private static final Condition conditionEven lock.newCondition();private static int count 0;public static void main(String[] args) {Thread producer new Thread(new Producer());Thread consumer1 new Thread(new ConsumerOdd(), Consumer-Odd);Thread consumer2 new Thread(new ConsumerEven(), Consumer-Even);producer.start();consumer1.start();consumer2.start();}static class Producer implements Runnable {Overridepublic void run() {while (true) {try {lock.lock();count;System.out.println(Produced: count);if (count % 2 0) {conditionEven.signal(); // Signal consumers waiting on even condition} else {conditionOdd.signal(); // Signal consumers waiting on odd condition}} finally {lock.unlock();}try {Thread.sleep(1000);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}}static class ConsumerOdd implements Runnable {Overridepublic void run() {while (true) {try {lock.lock();while (count % 2 0) {conditionOdd.await(); // Wait for odd number condition}// Process odd numberSystem.out.println(Thread.currentThread().getName() consumed: count);} catch (InterruptedException e) {Thread.currentThread().interrupt();} finally {lock.unlock();}}}}static class ConsumerEven implements Runnable {Overridepublic void run() {while (true) {try {lock.lock();while (count % 2 ! 0) {conditionEven.await(); // Wait for even number condition}// Process even numberSystem.out.println(Thread.currentThread().getName() consumed: count);} catch (InterruptedException e) {Thread.currentThread().interrupt();} finally {lock.unlock();}}}} }class MultiConditionExampleWithWaitNotify {private static final Object lock new Object();private static int count 0;public static void main(String[] args) {Thread producer new Thread(new Producer());Thread consumerOdd new Thread(new ConsumerOdd(), Consumer-Odd);Thread consumerEven new Thread(new ConsumerEven(), Consumer-Even);producer.start();consumerOdd.start();consumerEven.start();}static class Producer implements Runnable {Overridepublic void run() {while (true) {synchronized (lock) {count;System.out.println(Produced: count);if (count % 2 0) {lock.notifyAll(); // Notify consumers waiting on even condition} else {lock.notifyAll(); // Notify consumers waiting on odd condition}}try {Thread.sleep(1000);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}}static class ConsumerOdd implements Runnable {Overridepublic void run() {while (true) {synchronized (lock) {try {while (count % 2 0) {lock.wait(); // Wait for odd number condition}// Process odd numberSystem.out.println(Thread.currentThread().getName() consumed: count);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}}}static class ConsumerEven implements Runnable {Overridepublic void run() {while (true) {synchronized (lock) {try {while (count % 2 ! 0) {lock.wait(); // Wait for even number condition}// Process even numberSystem.out.println(Thread.currentThread().getName() consumed: count);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}}} }wait 和 notify只能使用单一的监视器锁并通过 notify 或 notifyAll 来通知等待线程。由于所有线程都在同一个锁对象上等待即使只需要唤醒部分线程例如只唤醒等待奇数的线程也必须通知所有等待的线程使用 notifyAll这样会导致非目标线程频繁被唤醒和再次等待。 Condition可以创建多个 Condition 实例在不同的条件队列上管理等待和通知。只会唤醒指定条件下的线程从而提高效率。 综合考虑 简洁性和安全性如果你的同步需求简单且不需要复杂的锁定逻辑选择 synchronized 更为简洁和安全。 灵活性和功能如果需要更灵活的锁控制、更高的性能、更多的特性如条件变量、尝试锁定、响应中断等ReentrantLock 提供更强大的功能。 文章转载自: http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.llsrg.cn.gov.cn.llsrg.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.jybj.cn.gov.cn.jybj.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.fjglf.cn.gov.cn.fjglf.cn http://www.morning.hytfz.cn.gov.cn.hytfz.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.klwxh.cn.gov.cn.klwxh.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.rrrrsr.com.gov.cn.rrrrsr.com http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.nsppc.cn.gov.cn.nsppc.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn 查看全文 http://www.tj-hxxt.cn/news/238807.html 相关文章: 常州网站建设公司如何公司网址怎么注册 Wordpress 建站 软件设计制作费用计入什么会计科目 抚顺网站建设招聘途牛网电子商务网站建设分析 什么软件做高级网站做网站.net和php哪个简单 网站文章优化流程方案网站技术制作流程图 石家庄模板自助建站上传本地wordpress至网络服务器 淮安网站建设 淮安网站制作珠海柏泰教育官方网站建设 音乐网站用dw怎么做商丘做网站哪个好 兰州建设局网站电信网络运营商 手机网站相册代码郏县建设局网站 做阀门网站效果怎么样优酷视频网站源码 做seo网站的步骤我是做装修的怎么样投资网站 电子商务网站建设与管理读书心得wordpress2345 网站后台不显示俄罗斯最新军事动态 湖南火电建设有限公司网站wordpress点击创建配置文件没反应 新城建站海南省住房和城乡建设局网站首页 建网站的网站做网站需要Excel表格吗 新建的网站怎么上首页oa系统多少钱一套 网站建设与管理广东药学院怎么制作网站链接手机 佛山仿站定制模板建站互联网巨头是哪几家 旅游网站策划书广东二次感染最新消息 网站开发宣传广告云建站的正确步骤 网站备案主体查询开发网页游戏平台 安康做网站的公司九江建筑工程有限公司 优秀的电商网站南京网站建设价位 海东企业网站建设公司顺企网杭州网站建设 男和男人怎么做那个视频网站wordpress伪 如何做网站客户案例龙岗网站建设企业 韩国免费行情网站的推荐理由wordpress 博客 注册 asp.net电子商务网站前台模板黑色网站模版