生产者消费者模式(队列描述)

他们共同操作的是size++这个操作,把size++用sychronized锁起来保证他的原子性
size用volatile修饰,保证他的可见性两者一起操作

class MyQueue2 {
    private int[] date = new int[100];
    private int head = 0;//队首元素的下标
    private int tail = 0;//队尾元素的下标
    //如果head == tail 则队列满了 因为这是个前闭后开区间

    private volatile int size = 0;

    //入队列
    public  void offer(int x) throws InterruptedException {
        if(size == date.length){
            //满了
            synchronized (this){
                wait();
            }

        }
        //把新元素放到tail的位置上
        date[tail] = x;
        tail++;
        if(tail == date.length){
            tail = 0;
        }
        synchronized (this) {
            size++;
            notify();
        }
    }

    //出队列
    public synchronized Integer poll() throws InterruptedException {
        if(size == 0){
            synchronized (this){
                wait();
            }
        }
        Integer ret = date[head];
        head++;
        if(head == date.length){
            head = 0;
        }
        synchronized (this) {
            size--;
            notify();
        }
        return ret;
    }
    public Integer peek(){
        if(size == 0){
            return null;
        }
        return date[head];
    }

    public boolean isEmpty(){
        return size == 0;
    }
    public  int size(){
        return size;
    }
    public static void main(String[] args) {
        Producer producer = new Producer();
        producer.start();
        Customer customer = new Customer();
        customer.start();
//        customer.sleep(100);
//        while (true){
//            System.out.println(queue2.size());
//        }
    }
    private static MyQueue2 queue2 = new MyQueue2();
    private static class Producer extends Thread{
        Producer(){
            super("生产者");
        }
        PrintWriter printWriter;
        {
            try {
                printWriter = new PrintWriter("生产者2.txt","UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            Random random = new Random(101);
            for (int i = 0; i < 1000; i++){
                try {
                    int val = random.nextInt(20);
                    queue2.offer(val);
                    printWriter.println(val);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            printWriter.close();
        }

    }
    private static class Customer extends Thread{
        Customer(){
            super("消费者");
        }
        PrintWriter printWriter;
        {
            try {
                printWriter = new PrintWriter("消费者.txt","UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++){
                try {
                    int val = queue2.poll();
                    printWriter.println(val);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            printWriter.close();
        }
    }
}

多消费者情况

class MyQueue2 {
    private int[] date = new int[100];
    private int head = 0;//队首元素的下标
    private int tail = 0;//队尾元素的下标
    //如果head == tail 则队列满了 因为这是个前闭后开区间

    private volatile int size = 0;

    //入队列
    public  synchronized void offer(int x) throws InterruptedException {
        while (size == date.length){
            //满了
                wait();

        }
        //把新元素放到tail的位置上
        date[tail] = x;
        tail++;
        if(tail == date.length){
            tail = 0;
        }
            size++;
            notify();
    }

    //出队列
    public synchronized Integer poll() throws InterruptedException {
        while (size == 0){
                wait();
        }
        Integer ret = date[head];
        head++;
        if(head == date.length){
            head = 0;
        }
            size--;
            notify();
        return ret;
    }
    public Integer peek(){
        if(size == 0){
            return null;
        }
        return date[head];
    }

    public boolean isEmpty(){
        return size == 0;
    }
    public  int size(){
        return size;
    }
    public static void main(String[] args) {
        Producer producer = new Producer();
        producer.start();
        Customer customer = new Customer();
        customer.start();
//        customer.sleep(100);
//        while (true){
//            System.out.println(queue2.size());
//        }
    }
    private static MyQueue2 queue2 = new MyQueue2();
    private static class Producer extends Thread{
        Producer(){
            super("生产者");
        }
        PrintWriter printWriter;
        {
            try {
                printWriter = new PrintWriter("生产者2.txt","UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            Random random = new Random(101);
            for (int i = 0; i < 1000; i++){
                try {
                    int val = random.nextInt(20);
                    queue2.offer(val);
                    printWriter.println(val);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            printWriter.close();
        }

    }
    private static class Customer extends Thread{
        Customer(){
            super("消费者");
        }
        PrintWriter printWriter;
        {
            try {
                printWriter = new PrintWriter("消费者.txt","UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++){
                try {
                    int val = queue2.poll();
                    printWriter.println(val);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            printWriter.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值