zoukankan      html  css  js  c++  java
  • 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进行操作
    */
    r1
  • 相关阅读:
    几个常用的url生成二维码的接口
    php 实现打印预览的功能
    php实现pdf导出和打印功能。
    PHP 写入缓存
    jQuery获取Select选择的Text和 Value(转)
    jQuery获取多种input值的方法
    php mysqli_get_server_version()函数
    大盘能涨多少点?
    让你懂起来
    楼市、股市后下一届ZF将用什么去做超发货币的蓄水池(
  • 原文地址:https://www.cnblogs.com/yhwsy/p/5816019.html
Copyright © 2011-2022 走看看