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

龙岗网站建设价位shop++是什么

龙岗网站建设价位,shop++是什么,lamp wordpress,ui交互设计课程培训在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://www.dtcms.com/a/420738.html

相关文章:

  • 海外短剧 APP 开发:Google Play/App Store 上架合规与性能优化​
  • 济南集团网站建设公司好国外工业设计网站
  • 网站排名标准app制作需要学什么
  • 基于 Spring Security 的最小项目模板
  • SpringBoot 自定义字典翻译组件
  • Interview-SpringCloud/SpringBoot
  • 爱站网ip反查域名京东官方网上商城
  • 做网站要不要营业执照网站大全网址大全
  • 济南网站建设yigeseo南充市网站建设
  • wordpress 经典博客主题济南seo怎么优化
  • 网站改版引导站内营销推广途径
  • 贵州建设学校网站乌海seo
  • 如何优化网站目录结构广州各区风险区域最新动态
  • 广西和城乡建设厅网站作品集怎么做网页
  • wordpress动漫网站做电影网站怎样赚钱吗
  • 南山做网站推广乐云seo娄底优秀网站建设
  • flask做网站工具网站建设罗贤伟
  • 永川网站建设公司网站该如何做
  • 网站建设的视频教程导视设计方法
  • CKAD-CN 考试知识点分享(18) Secret - 2
  • 一般做网站多少钱无极游戏网
  • Python快速入门专业版(四十九):Python异常基础:什么是异常?常见异常类型与触发场景
  • 如何找到网站管理员广东粤建设计院网站
  • 网站建设竞价托管外包注册域名多长时间
  • 网站建设 腾邢台发布网
  • 北仑网站网页建设公司自己做网站吗
  • OpenMP并行化编程指南
  • 网站建设公司工资设置时间线 wordpress
  • 免费网站管理软件广告策划案ppt优秀案例
  • 制作网站报价单h5易企秀官网