• 生产者 和 消费者 1


    package com..utils.threads;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ProdCons {
    
        public static void main(String[] args) {
            Queue queue = new Queue();
            new Thread(new Producer(queue)).start();
            new Thread(new Consumer(queue)).start();
    //        new Thread(new Consumer(queue)).start();//再起一个消费者
        }
    
        static class Producer implements Runnable {
    
            Queue queue;
    
            Producer(Queue queue) {
                this.queue = queue;
            }
    
            @Override
            public void run() {
                try {
                    for (int i = 1; i < 10000; i++) {
                        Thread.sleep(1000);
                        queue.putEle(i);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        static class Consumer implements Runnable {
    
            Queue queue;
    
            Consumer(Queue queue) {
                this.queue = queue;
            }
    
            @Override
            public void run() {
                try {
                    for (int i = 1; i < 10000; i++) {
                        Thread.sleep(2000);
                        queue.takeEle();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        static class Queue {
            Lock lock = new ReentrantLock();
            Condition prodCond  = lock.newCondition();
            Condition consCond = lock.newCondition();
    
            final int CAPACITY = 5;
            Object[] container = new Object[CAPACITY];
            int count = 0;
            int putIndex = 0;
            int takeIndex = 0;
    
            public void putEle(Object ele) throws InterruptedException {
                try {
                    lock.lock();
                    while (count == CAPACITY) {
                        System.out.println(String.format("队列已满:%d,生产者开始 Seelp。。。", count));
                        prodCond.await();
                    }
                    container[putIndex] = ele;
                    System.out.println(String.format("生产元素:%d", ele));
                    putIndex++;
                    if (putIndex >= CAPACITY) {
                        putIndex = 0;
                    }
                    count++;
                    System.out.println(String.format("队列已有 :"+String.valueOf(count)));
                    consCond.signalAll();
                } finally {
                    lock.unlock();
                }
            }
    
            public Object takeEle() throws InterruptedException {
                try {
                    lock.lock();
                    while (count == 0) {
                        System.out.println(String.format("队列已空:%d,消费者开始 Seelp。。。", count));
                        consCond.await();
                    }
                    Object ele = container[takeIndex];
                    System.out.println(String.format("消费元素:%d", ele));
                    takeIndex++;
                    if (takeIndex >= CAPACITY) {
                        takeIndex = 0;
                    }
                    count--;
                    System.out.println(String.format("队列剩余 :" +String.valueOf(count)));
                    prodCond.signalAll();
                    return ele;
                } finally {
                    lock.unlock();
                }
            }
        }
    
    
    }
  • 相关阅读:
    Chrome技巧
    jQuery中.bind() .live() .delegate() .on()的区别
    BRAVEHEART勇敢的心威廉姆华莱士战场演讲
    CSS3小模块hover左右交替互换动画
    sublime text 3
    百度图片搜索页的图片展示列表模块jquery效果
    出埃及记:摩西劈开红海
    用位数组计算整数中1的个数
    Using the XPath Wrappers
    【转】为Xcode 4挑选自己喜欢的字体和颜色(Panic Sans)
  • 原文地址:https://www.cnblogs.com/lifan12589/p/14591512.html
Copyright © 2020-2023  润新知