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

浅谈中兴电子商务网站建设html考试界面设计

浅谈中兴电子商务网站建设,html考试界面设计,做网站后台数据库建设,成都公司建设网站在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/485951.html

相关文章:

  • 工业三防平板背后的条码与RFID采集技术
  • pytorch框架GPU适配npu
  • 【散列函数】哈希函数简介
  • 学英语音标作用,能听出声音拼音组成,记忆效率提高
  • 学习日记day
  • Python爬虫数据可视化:深度分析贝壳成交价格趋势与分布
  • C++中的父继子承(2)多继承菱形继承问题,多继承指针偏移,继承组合分析+高质量习题扫尾继承多态
  • 做公司网站别人能看到吗6网站源码传到服务器上后怎么做
  • php多语言网站开发网站界面设计图片
  • 树形结构渲染 + 选择(Vue3 + ElementPlus)
  • Redis技术应用
  • hot100练习-8
  • 手机网站设置在哪里找房产信息平台
  • 算法入门:专题二---滑动窗口(长度最小的子数组)更新中
  • 2025年存储市场报告深度解读
  • HTTP 413 状态码详解与前端处理,请求体过大
  • 大数据背景下时序数据库选型指南:国产开源技术的突破与实践
  • asp网站优化云南网站制作需求
  • k8s(六)Pod的资源控制器
  • TypeScript前端架构与开发技巧深度解析:从工程化到性能优化的完整实践
  • 郴州做网站网站建设公司ejiew
  • LeetCode 将数组和减半的最少操作次数
  • OpenHarmony南向开发环境搭建 - 深入理解Ubuntu、DevEco Device Tool与HPM
  • QT-day1
  • Spec-Kit+Copilot打造AI规格驱动开发
  • Linux服务器编程实践30-TCP交互数据流:Nagle算法与延迟确认的作用
  • MATLAB一个基于Attention-LSTM的分类模型,构建Attention-LSTM深度学习模型,训练模型并进行分类预测
  • 杭州网站建设朗诵面朝网站建设策划内容
  • 手机网站开发模板南昌网站设计建设
  • Playwright中page的实现类深度解析-PageImpl 方法作用解析