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

海城建设网站免费有效的推广网站

海城建设网站,免费有效的推广网站,社交网站建设,信息发布在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/wzjs/381121.html

相关文章:

  • 团购网站建设目的申请网站怎么申请
  • 做政协网站的目的是什么有道搜索引擎入口
  • 珠海移动网站建设费用近期国际新闻热点大事件
  • 国外做问卷调查的网站查询网站流量
  • 如何办理网站实时军事热点
  • 网站开发公司首页济南优化网站的哪家好
  • 响应式网站建设平台seo排名优化方式
  • 兰州市网站建设公司seo综合查询是什么
  • 做网站会出现什么问题线上广告平台
  • 浙江恒炜建设网站教育培训机构
  • 炫酷网站代码专门做排行榜的软件
  • 宁波seo公司网站推广室内设计培训班学费一般多少
  • 深圳网站设计与开发怎么宣传自己的产品
  • 网站建设报告seo高手培训
  • 贵州水利建设官方网站站长工具seo综合查询怎么用
  • 建设行业的门户网站外贸网站都有哪些
  • 2003 iis网站发布网站网络营销专业培训学校
  • 微信网站建设价格广告联盟怎么加入
  • 广州建设网站网络营销整合推广
  • 网站怎么弄外贸互联网推广的
  • 做网站业务员提成几个点湖南优化推广
  • 网站伪静态seo关键词优化举例
  • 智能网站建设设计媒体资源网官网
  • 有名做网站公司免费域名注册永久
  • 高端网站建设的市场广东网站se0优化公司
  • 网站做用户记录软文营销的技巧
  • 任丘网站优化搜索引擎营销案例分析
  • 做中介卖房子开哪个网站理发培训专业学校
  • 桂林旅游网官方网站google ads
  • 如何降低网站的权重上海seo顾问