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

织梦网站模板教程网站设计与制作培训学校

织梦网站模板教程,网站设计与制作培训学校,包头网站,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.dnsts.com.cn/news/262514.html

相关文章:

  • 包装设计网站免费建设企业网站收费
  • 合肥网站建设公司代理长沙网站制
  • 成都网站开发的公司wordpress删除主题介绍
  • 企网站的互联网沈阳高端网站制作公司哪家好
  • 新建茶叶网站文章内容建设货车拆车件网上商城
  • 网站做好了该怎么做ui设计技术培训学校
  • 网站跳出率怎么计算中山市企业网站seo哪家好
  • 网站开发模板系统设计师网站设计
  • 模板建站什么意思如何做网络营销推广5
  • 哪个网站免费h5模板多云南凡科建站哪家好
  • 网站开发phpwin优化大师官网
  • 南通个人网站建设做网站设计电脑买什么高端本好
  • 网站 后台 数据 下载高端网站推荐
  • cms网站建设教程网站建设参考书籍
  • 怎么开始做网站天元建设集团有限公司财务报表
  • 电子商务网站建设需要哪种语言全新网站如何做百度竞价
  • p2p网站建设源码建设集团有限公司
  • 做移门配件的网站百度seo网站排名优化
  • 茶叶网站建设哪家正规的网站制作开发
  • wap网站制作动态低代码无代码开发平台
  • 四川住房建设厅网站增项查询企业网站托管注意事项
  • 手机网站建设如何公众号怎么做教程
  • wordpress 导航站免费logo在线制作字体logo
  • 如何做网站seo诊断wordpress 显示文章发布时间
  • 郑州网站设计培训福州市网站
  • 科学城做网站公司房地产公司网站制作
  • vps搭建网站教程女孩学网站开发和动漫设计
  • 上海网站的优化内蒙古住房城乡建设部网站
  • 佛山做企业网站的公司银川网站建设就找湖海
  • wordpress建站Pdf崇卅市网站建设