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

青岛公路建设集团有限公司网站资讯网站模版

青岛公路建设集团有限公司网站,资讯网站模版,wordpress 搜索本站,石家庄网站开发工程师招聘网在Java中,生产者-消费者模型可以通过多种方式实现。以下是常见的几种实现方法及其代码示例: 1. 使用 wait() 和 notify()(基础同步机制) 通过 synchronized 块和 Object 的等待/唤醒机制实现。 public class WaitNotifyExample…

在Java中,生产者-消费者模型可以通过多种方式实现。以下是常见的几种实现方法及其代码示例:


 1. 使用 `wait()` 和 `notify()`(基础同步机制)
通过 `synchronized` 块和 `Object` 的等待/唤醒机制实现。
 

public class WaitNotifyExample {private final Queue<Integer> queue = new LinkedList<>();private final int MAX_SIZE = 10;public void produce() throws InterruptedException {while (true) {synchronized (queue) {while (queue.size() == MAX_SIZE) {queue.wait(); // 队列满时等待}int value = new Random().nextInt(100);queue.add(value);System.out.println("生产: " + value);queue.notifyAll(); // 唤醒消费者}Thread.sleep(500);}}public void consume() throws InterruptedException {while (true) {synchronized (queue) {while (queue.isEmpty()) {queue.wait(); // 队列空时等待}int value = queue.poll();System.out.println("消费: " + value);queue.notifyAll(); // 唤醒生产者}Thread.sleep(1000);}}
}

 2. 使用 `BlockingQueue`(线程安全队列)
直接利用 `BlockingQueue` 的阻塞特性简化代码。

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;public class BlockingQueueExample {private final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);public void produce() throws InterruptedException {while (true) {int value = new Random().nextInt(100);queue.put(value); // 队列满时自动阻塞System.out.println("生产: " + value);Thread.sleep(500);}}public void consume() throws InterruptedException {while (true) {int value = queue.take(); // 队列空时自动阻塞System.out.println("消费: " + value);Thread.sleep(1000);}}
}

 3. 使用 `Lock` 和 `Condition`(更灵活的锁)
通过显式锁和条件变量实现细粒度控制。

import java.util.concurrent.locks.*;
import java.util.Queue;
import java.util.LinkedList;public class LockConditionExample {private final Queue<Integer> queue = new LinkedList<>();private final int MAX_SIZE = 10;private final Lock lock = new ReentrantLock();private final Condition notFull = lock.newCondition();private final Condition notEmpty = lock.newCondition();public void produce() throws InterruptedException {while (true) {lock.lock();try {while (queue.size() == MAX_SIZE) {notFull.await(); // 等待队列不满}int value = new Random().nextInt(100);queue.add(value);System.out.println("生产: " + value);notEmpty.signal(); // 唤醒消费者} finally {lock.unlock();}Thread.sleep(500);}}public void consume() throws InterruptedException {while (true) {lock.lock();try {while (queue.isEmpty()) {notEmpty.await(); // 等待队列不空}int value = queue.poll();System.out.println("消费: " + value);notFull.signal(); // 唤醒生产者} finally {lock.unlock();}Thread.sleep(1000);}}
}

 4. 使用 `Semaphore`(信号量控制资源)
通过信号量管理可用资源数量。

import java.util.concurrent.Semaphore;
import java.util.Queue;
import java.util.LinkedList;public class SemaphoreExample {private final Queue<Integer> queue = new LinkedList<>();private final int MAX_SIZE = 10;private final Semaphore semProducer = new Semaphore(MAX_SIZE);private final Semaphore semConsumer = new Semaphore(0);private final Object lock = new Object();public void produce() throws InterruptedException {while (true) {semProducer.acquire(); // 获取生产许可synchronized (lock) {int value = new Random().nextInt(100);queue.add(value);System.out.println("生产: " + value);}semConsumer.release(); // 释放消费许可Thread.sleep(500);}}public void consume() throws InterruptedException {while (true) {semConsumer.acquire(); // 获取消费许可synchronized (lock) {int value = queue.poll();System.out.println("消费: " + value);}semProducer.release(); // 释放生产许可Thread.sleep(1000);}}
}

总结
以上四种是Java中实现生产者-消费者的主流方式:
1. `wait()/notify()`:适合基础场景,需手动处理同步。
2. `BlockingQueue`:代码最简洁,推荐优先使用。
3. `Lock` + `Condition`:提供更灵活的锁控制。
4. `Semaphore`:通过资源计数管理同步,需注意线程安全。

根据具体需求(如性能、复杂度、可扩展性)选择合适的方式。


文章转载自:

http://FEyGsoAM.nLLst.cn
http://7OEsDTvC.nLLst.cn
http://9MQ09RgI.nLLst.cn
http://oNyVh4Bw.nLLst.cn
http://rz9CC9op.nLLst.cn
http://FSn4v5aF.nLLst.cn
http://zMQuz86p.nLLst.cn
http://UzgFEJaw.nLLst.cn
http://KcUtgrXH.nLLst.cn
http://nD2ZYprp.nLLst.cn
http://szbMhG8n.nLLst.cn
http://fg4XjBZf.nLLst.cn
http://sO4dWMxg.nLLst.cn
http://Bvale2ir.nLLst.cn
http://ZwXi3ANS.nLLst.cn
http://w9AjCYc0.nLLst.cn
http://68kINTEA.nLLst.cn
http://WQFJ5t7B.nLLst.cn
http://9JXFzb7d.nLLst.cn
http://jEEezONN.nLLst.cn
http://lCIa1iJ4.nLLst.cn
http://QZF2sB9u.nLLst.cn
http://6QSeNzlB.nLLst.cn
http://E0aATLBk.nLLst.cn
http://js8bvAva.nLLst.cn
http://nHMw0BMu.nLLst.cn
http://1InRTELE.nLLst.cn
http://n0EWqfMh.nLLst.cn
http://g4rrJcUj.nLLst.cn
http://8LLezLWW.nLLst.cn
http://www.dtcms.com/wzjs/645112.html

相关文章:

  • api网站网址大全seo网站提交
  • 哈尔滨工程建设信息网站创意logo图片
  • 大庆市建设网站wordpress怎么设置伪静态页面
  • 深圳网站建设公司业务招聘桂林象鼻山门票
  • 网站兼容哪些浏览器网站开发的权限设置
  • 网站建设公司发展建议普洱市住房和城乡建设局网站
  • php 上传到网站电销助手app
  • 如何做适合手机访问的网站wordpress 编辑器模板
  • 林业网站源码用asp做的网站如何运行
  • 网站子站建设合同样本黄冈建设局网站
  • 代做单片机毕业设计网站电子商务营销模式有哪些
  • 广东知名网站wordpress添加表格
  • 网站建设财务计划与预测开发公司和建筑公司同一法人
  • 网站移动端指的是什么在线画画网站
  • 拖拽建站平台关键词优化策略
  • 北京网站设计制作费用网站前后端的关系
  • 电商网站功能模块图江苏建设信息网官网
  • 无锡建设网站的公司哪家好网站开发的课程设置
  • 福州建设高端网站正规的网站建设专业公司
  • 网站策划模板跨境电商怎么样
  • 福州网站建设策划方案如何做可以微信转发的网站
  • 企业网站是企业在互联网上进行网络营销深圳专业优定软件网站建设
  • 网站页头尺寸案例展示网站
  • jsp和servlet网站开发网站模板怎么建设
  • 网站icp备案有效时间网站建设留言板的实现
  • 国外的app设计网站四川省住房与建设厅网站
  • 免费的免抠图素材网站成立一间网站开发公司
  • 网页设计新手制作的网站代码jsp网站建设代码
  • google建设网站赚钱品牌网是什么网站
  • 网站开发专业术语大连制作网站报价