java 学习笔记 生产者与消费者

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 学习笔记 生产者与消费者相关的知识,希望对你有一定的参考价值。

Object类对线程的支持——等待与唤醒

public final void wait() throws InterruptedException

public final void notify()

public final void notifyAll()

public class Test {
    public static void main(String[] args) {
         Basket basket = new Basket();
         
         Producter p1 = new Producter("p1",basket);
         Producter p2 = new Producter("p2",basket);
         Consumer c1 = new Consumer("c1",basket);
         Consumer c2 = new Consumer("c2",basket);    
         
         Thread t1 = new Thread(p1);        
         Thread t2 = new Thread(p2);
         Thread t3 = new Thread(c1,"c1");
         Thread t4 = new Thread(c2,"c2");
         
         t1.start();
         t2.start();
         t3.start();
         t4.start();
    }    
}

//定义面包类
class Bread {
    int id;
    String info;
    
    Bread (String info,int id) {
        this.info = info;    
        this.id = id;
    }
}

//定义容器类
class Basket {
    int index = 0;//先进先出型
    Bread bread;
    Bread br[] = new Bread[10];//定义一个数组,容量为6
    
    public synchronized void putIn(Bread bread){
        while (index == br.length) {
            try {
                this.wait();    
            }catch(InterruptedException e) {
                System.out.println("wait中被打断");    
            }
        }
        this.notify();
        br[index] = bread;    
        index++;
        System.out.println("(" + bread.info + "生产第" + bread.id + "个)  " + "现有" + this.index + "个");    
    }
    
    public synchronized void takeOut(){
        while(index == 0 ) {
            try {
                this.wait();    
            }catch(InterruptedException e) {
                System.out.println("wait中被打断");    
            }    
        }
        this.notify();
        index--;
        System.out.println(Thread.currentThread().getName() + "取走   " +"还剩" + index + " ");
    }        
}

//定义生产者类
class Producter implements Runnable {
    Basket basket = null;
    String name = null;
    int j = 0;

    public Producter(String name,Basket basket) {
        this.name = name;    
        this.basket = basket;
    }
    
    public void run() {
        for (int i = 0;i <= 15;i++) {
            j = i + 1;
            Bread bread = new Bread(this.name,j);//不需要成员变量含有Bread类 ,直接new出来
            basket.putIn(bread);
        }    
    }        
}

//定义消费者类
class Consumer implements Runnable{
    String name;
    Basket basket;
    
    public Consumer(String name,Basket basket){
        this.name = name;
        this.basket = basket;
    }
    
    public void run() {
        for (int i = 0;i <= 15;i++) {
            basket.takeOut();
        }    
    }    
}

/*
1.在 for语句循环内,不要对循环变量i进行操作
*/
技术分享

以上是关于java 学习笔记 生产者与消费者的主要内容,如果未能解决你的问题,请参考以下文章

消息中间件ActiveMQ学习笔记 [Java编码MQ,消费者生产者基本模型]

SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段

Java学习:同步问题之生产者与消费者的问题

java并发编程实战读书笔记4--生产者消费者模式和队列

java并发编程实战读书笔记4--生产者消费者模式和队列

“全栈2019”Java多线程第二十五章:生产者与消费者线程详解