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
  • 相关阅读:
    POJ 3468 A Simple Problem with Integers
    BZOJ 4430 Guessing Camels
    POJ 2309 BST
    POJ 1990 MooFest
    cf 822B Crossword solving
    cf B. Black Square
    cf 828 A. Restaurant Tables
    Codefroces 822C Hacker, pack your bags!
    [HDU 2255] 奔小康赚大钱
    [BZOJ 1735] Muddy Fields
  • 原文地址:https://www.cnblogs.com/yhwsy/p/5816019.html
Copyright © 2011-2022 走看看