生产者消费者模型java实现(一)

本文介绍了一个典型的生产者-消费者模式实现案例,通过Java代码展示了如何利用线程同步机制来确保生产和消费过程的正确性。该模式适用于解决多线程环境中资源的生产和消费问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Producer implements Runnable {  
 
    private String producerName = null; 
 
    private StoreHouse storeHouse = null; 
 
    public Producer(String producerName, StoreHouse storeHouse) { 
        this.producerName = producerName; 
        this.storeHouse = storeHouse; 
    } 
 
    public void setProducerName(String producerName) { 
        this.producerName = producerName; 
    } 
 
    public String getProducerName() { 
        return producerName; 
    } 
     
    public void produceProduct() { 
        int i = 0; 
        while (true) { 
            i++; 
            Product pro = new Product(i); 
            storeHouse.push(pro); 
            System.out.println(getProducerName() + " 生产了 " + pro); 
            try { 
                Thread.sleep(2000); 
            } catch (InterruptedException e) { 
                return; 
            } 
        } 
    } 
 
    public void run() { 
        produceProduct(); 
    } 
 


class Consumer implements Runnable { 
     
    private String consumerName = null; 
     
    private StoreHouse storeHouse = null; 
 
    public Consumer(String consumerName, StoreHouse storeHouse) { 
        this.consumerName = consumerName; 
        this.storeHouse = storeHouse; 
    } 
     
    public void setConsumerName(String consumerName) { 
        this.consumerName = consumerName; 
    } 
     
    public String getConsumerName() { 
        return consumerName; 
    } 
     
    public void consumerProduct() { 
        while (true) { 
            System.out.println(getConsumerName() + " 消费了 " + storeHouse.pop()); 
            try { 
                Thread.sleep(5000); 
            } catch (InterruptedException e) { 
                return; 
            } 
        } 
    } 
 
    public void run() { 
        consumerProduct(); 
    } 
 



 
class Product { 
 
    private int productId = 0; 
     
    public Product(int productId) { 
        this.productId = productId; 
    } 
 
    public int getProductId() { 
        return productId; 
    } 
 
    public String toString() { 
        return Integer.toString(productId); 
    } 


class StoreHouse { 
 
    private int base = 0; 
 
    private int top = 0; 
 
    private Product[] products = new Product[10]; 
 
    public synchronized void push(Product product) { 
        while (top == products.length) { 
            notify(); 
            try { 
                System.out.println("仓库已满,正等待消费..."); 
                wait(); 
            } catch (InterruptedException e) { 
                System.out.println("stop push product because other reasons"); 

            } 
        } 
        products[top] = product; 
        top++; 
    } 
 
    public synchronized Product pop() { 
        Product pro = null; 
        while (top == base) { 
            notify(); 
            try { 
                System.out.println("仓库已空,正等待生产..."); 
                wait(); 
            } catch (InterruptedException e) { 
                System.out.println("stop push product because other reasons"); 

            } 
        } 
        top--; 
        pro = products[top]; 
        products[top] = null; 
        return pro; 
    } 


public class TestPC { 
    public static void main(String[] args) { 
        StoreHouse storeHouse = new StoreHouse(); 
        Producer producer = new Producer("生产者", storeHouse); 
        Consumer comsumer = new Consumer("消费者", storeHouse); 
        Thread t1 = new Thread(producer); 
        Thread t2 = new Thread(comsumer); 
        t1.start(); 
        t2.start(); 
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值