首页 > 代码库 > 生产者消费者的java实现

生产者消费者的java实现

先看最简单的,也就是缓冲区的容量为1

缓冲区容量为1

import java.util.List;

public class ProducerAndConsumer2 {
    static class AddThread implements Runnable {
        Plate plate;

        public AddThread(Plate p) {
            this.plate = p;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            plate.put(new Object());
        }
    }

    static class GetThread implements Runnable {
        Plate plate;

        public GetThread(Plate p) {
            this.plate = p;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            plate.get();
        }
    }

    static class Plate {
        private List<Object> egg = new ArrayList<Object>();

        public synchronized void put(Object o) {
            while (egg.size() > 0) {
                try {
                    System.out.println(
                            "此时盘子里有鸡蛋    生产线程"+Thread.currentThread().getName()+"阻塞");
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            egg.add(0, o);
            System.out.println("生产线程放入一个鸡蛋");
            this.notifyAll();
        }
        
        public synchronized Object get() {
            while (egg.size() == 0) {
                try {
                    System.out.println(
                            "此时盘子里没有鸡蛋   消费线程"+Thread.currentThread().getName()+"阻塞");
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            Object object = egg.get(0);
            egg.clear();
            System.out.println("消费线程取得一个鸡蛋");
            this.notifyAll();
            return object;
        }
    }

    public static void main(String[] args) {
        Plate p = new Plate();
        for (int i = 0; i < 10; i++)
            new Thread(new GetThread(p)).start();

        for (int i = 0; i < 10; i++)
            new Thread(new AddThread(p)).start();
    }
}



如果盘子里可以存放多个鸡蛋就这样:

缓冲器容量大于1

package cn.itcast.heima2;

import java.util.ArrayList;
import java.util.List;

public class ProducerAndConsumer {
    public static void main(String[] args) {
        Plate p = new Plate();
        
        for (int i = 0; i < 10; i++)
            new Thread(new GetThread(p)).start();
        for (int i = 0; i < 10; i++)
            new Thread(new AddThread(p)).start();
    }

}

class AddThread implements Runnable {
    Plate plate;

    public AddThread(Plate p) {
        this.plate = p;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        plate.put(new Object());
    }
}

class GetThread implements Runnable {
    Plate plate;

    public GetThread(Plate p) {
        this.plate = p;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        plate.get();
    }
}

class Plate {

    private List<Object> egg = new ArrayList<Object>(10);
                                   //在0-9这十个空间存储鸡蛋
    public synchronized void put(Object o) {
        while (egg.size()==10) { //最多有是个鸡蛋
            try {
                System.out.println(
                        "此时盘子里有鸡蛋    生产线程"+Thread.currentThread().getName()+"阻塞");
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        egg.add(o);
        System.out.println("生产线程放入一个鸡蛋 现有鸡蛋"+egg.size()+"个");
        this.notifyAll();
    }

    public synchronized Object get() {
        while (egg.size()==0) {
            try {
                System.out.println(
                        "此时盘子里没有鸡蛋   消费线程"+Thread.currentThread().getName()+"阻塞");
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        Object object=egg.get(egg.size()-1);
        egg.remove(egg.size()-1);
        System.out.println("消费线程取得一个鸡蛋  还剩余鸡蛋"+egg.size()+"个");
        return object;
    }

}

感谢glt

参考资料

http://blog.csdn.net/ghsau/article/details/7433673
http://blog.csdn.net/monkey_d_meng/article/details/6251879

生产者消费者的java实现