zoukankan      html  css  js  c++  java
  • JAVA多线程学习--生产者消费者问题

    一、问题描述

      生产者消费者问题是一个典型的线程同步问题。生产者生产商品放到容器中,容器有一定的容量(只能顺序放,先放后拿),消费者消费商品,当容器满了后,生产者等待,当容器为空时,消费者等待。当生产者将商品放入容器后,通知消费者;当消费者拿走商品后,通知生产者。

    二、解决方案

      对容器资源加锁,当取得锁后,才能对互斥资源进行操作。

    public class ProducerConsumerTest {
        
        public static void main(String []args){
            Container con = new Container();
            Producer p = new Producer(con);
            Consumer c = new Consumer(con);
            new Thread(p).start();
            new Thread(c).start();
        }
    
    }
    
    
    class Goods{
        int id;
        public Goods(int id){
            this.id=id;
        }
        
        public String toString(){
            return "商品"+this.id;
        }
    }
    
    class Container{//容器采用栈,先进后出
        private int index = 0;
        Goods[] goods = new Goods[6];
        
        public synchronized void push(Goods good){
            while(index==goods.length){//当容器满了,生产者等待
                try {
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            goods[index]=good;
            index++;
            notifyAll();//当生产者放入商品后通知消费者
        }
        
        public synchronized Goods pop(){
            while(index==0){//当容器内没有商品是等待
                try {
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            index--;
            notifyAll();//当消费者消费了商品后通知生产者
            return goods[index];
        }
    }
    
    class Producer implements Runnable{
        
        Container con = new Container();
        public Producer(Container con){
            this.con=con;
        }
        
        public void run(){
            for(int i=0; i<20; i++){
                Goods good = new Goods(i);
                con.push(good);
                System.out.println("生产了:"+good);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    class Consumer implements Runnable{
        
        Container con = new Container();
        public Consumer(Container con){
            this.con=con;
        }
        
        public void run(){
            for(int i=0; i<20; i++){
                Goods good=con.pop();
                System.out.println("消费了:"+good);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }
  • 相关阅读:
    ABAP接口用法
    监听textarea数值变化
    The first step in solving any problem is recognizing there is one.
    Wrinkles should merely indicate where smiles have been.
    God made relatives.Thank God we can choose our friends.
    Home is where your heart is
    ABAP跳转屏幕
    Python 工具包 werkzeug 初探
    atom通过remote ftp同步本地文件到远程主机的方法
    Mongodb学习笔记一
  • 原文地址:https://www.cnblogs.com/vettel/p/3438990.html
Copyright © 2011-2022 走看看