实现生产者消费者问题

package com.bzw.multithreaded;

import java.util.LinkedList;
import java.util.Queue;

public class ProducedCustom {

    public static void main(String[] args) {

        Produce produce = new Produce();

        Producer producer = new Producer(produce);
        Customer customer = new Customer(produce);
        producer.start();
        customer.start();


    }


}

class Produce{

    private final static int size = 20;
    Queue<Integer> queue = new LinkedList<>();
    public synchronized void put(int i){
        if (queue.size() >= size ){
            System.out.println("仓库已满,停止生产");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "生产者生产了第" + i + "件商品");
        queue.add(i);
        notify();
    }

    public synchronized int get(){
        if (queue.isEmpty()){
            System.out.println("库存为空,停止销售");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int val = queue.poll();
        System.out.println(Thread.currentThread().getName() + "消费者购买了第" + val + "件商品");
        notify();
        return val;
    }

}

class Producer extends Thread{

    private Produce produce;

    public Producer(Produce produce){
        this.produce = produce;
    }
    public void run(){
        for (int i=0;i<=40;i++){
            produce.put(i);
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }
}

class Customer extends Thread{
    private Produce produce;

    public Customer(Produce produce){
        this.produce = produce;
    }
    public void run(){
        while (true){
            produce.get();
        }

    }
}

  这里用来一个Produce类表示仓库,用队列来存储数据,生产者和消费者都是通过仓库间接通信。当仓库满时,让生产者wait();当仓库空时,让消费者wait()。在消费者和生产者中的run方法中都是调用仓库中的方法,实现数据共享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生产者消费者问题是一个经典的并发问题,它涉及到多个进程或线程之间的同步和互斥。在C语言中,可以使用线程和信号量来实现生产者消费者问题。 具体实现步骤如下: 1. 定义一个共享的缓冲区,可以使用队列或环形缓冲区。 2. 定义一个生产者线程和一个消费者线程。 3. 在生产者线程中,不断生成随机数并将其添加到缓冲区内,并在此之后将 full 计数器加一。 4. 在消费者线程中,检索当前可用的缓冲区,如果满足条件,则从计数器中减去 1,并从缓冲区中读取数据,并在满足特定条件时输出该数据。 5. 使用信号量来实现同步和互斥,保证在生产者没有向缓冲区提交任何内容时,消费者不会读取缓冲区中的任何数字。 下面是一个简单的C语言实现生产者消费者问题的代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in = 0; int out = 0; int count = 0; sem_t empty; sem_t full; pthread_mutex_t mutex; void *producer(void *arg) { int item; while (1) { item = rand() % 100; // 生成随机数 sem_wait(&empty); // 等待空缓冲区 pthread_mutex_lock(&mutex); // 加锁 buffer[in] = item; in = (in + 1) % BUFFER_SIZE; count++; printf("Producer produced item %d\n", item); pthread_mutex_unlock(&mutex); // 解锁 sem_post(&full); // 发送满缓冲区信号 } } void *consumer(void *arg) { int item; while (1) { sem_wait(&full); // 等待满缓冲区 pthread_mutex_lock(&mutex); // 加锁 item = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; if (item % 2 == 0) { printf("Consumer consumed item %d\n", item); } pthread_mutex_unlock(&mutex); // 解锁 sem_post(&empty); // 发送空缓冲区信号 } } int main() { pthread_t producer_thread, consumer_thread; sem_init(&empty, 0, BUFFER_SIZE); sem_init(&full, 0, 0); pthread_mutex_init(&mutex, NULL); pthread_create(&producer_thread, NULL, producer, NULL); pthread_create(&consumer_thread, NULL, consumer, NULL); pthread_join(producer_thread, NULL); pthread_join(consumer_thread, NULL); sem_destroy(&empty); sem_destroy(&full); pthread_mutex_destroy(&mutex); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值