zoukankan      html  css  js  c++  java
  • 理解Condition的用法

    这个示例中BoundedBuffer是一个固定长度的集合,这个在其put操作时,如果发现长度已经达到最大长度,那么会等待notFull信号,如果得到notFull信号会像集合中添加元素,并发出notEmpty的信号,而在其take方法中如果发现集合长度为空,那么会等待notEmpty的信号,同时如果拿到一个元素,那么会发出notFull的信号。

    package locks;
    
    
    
    
    import java.util.Random;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    
    
    public class AppOfficial {
    
        /**
         * BoundedBuffer 是一个定长100的集合,当集合中没有元素时,take方法需要等待,直到有元素时才返回元素
         * 当其中的元素数达到最大值时,要等待直到元素被take之后才执行put的操作
         * @author yukaizhao
         *
         */
        static class BoundedBuffer {
            final Lock lock = new ReentrantLock();
            final Condition notFull = lock.newCondition();
            final Condition notEmpty = lock.newCondition();
    
            final Object[] items = new Object[100];
            int putptr, takeptr, count;
    
            public void put(Object x) throws InterruptedException {
                System .out.println("put wait lock");
                lock.lock();
                System.out.println("put get lock");
                try {
                    while (count == items.length) {
                        System.out.println("buffer full, please wait");
                        notFull.await();
                    }
                        
                    items[putptr] = x;
                    if (++putptr == items.length)
                        putptr = 0;
                    ++count;
                    notEmpty.signal();
                } finally {
                    lock.unlock();
                }
            }
    
    
    
    
            public Object take() throws InterruptedException {
                System.out.println("take wait lock");
                lock.lock();
                System.out.println("take get lock");
                try {
                    while (count == 0) {
                        System.out.println("no elements, please wait");
                        notEmpty.await();
                    }
                    Object x = items[takeptr];
                    if (++takeptr == items.length)
                        takeptr = 0;
                    --count;
                    notFull.signal();
                    return x;
                } finally {
                    lock.unlock();
                }
            }
        }
        
        public static void main(String[] args) {
            final BoundedBuffer boundedBuffer = new BoundedBuffer();
            
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("t1 run");
                    for (int i=0;i<1000;i++) {
                        try {
                            System.out.println("putting..");
                            boundedBuffer.put(Integer.valueOf(i));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                
            }) ;
            
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<1000;i++) {
                        try {
                            Object val = boundedBuffer.take();
                            System.out.println(val);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                
            }) ;
            
            t1.start();
            t2.start();
        }
    }
  • 相关阅读:
    formValidator表单验证
    js中判断一个对象的类型的种种方法
    利用jQuery中的serialize方法大量获取页面中表单的数据,发送的服务器
    用html和css写一个头部header和左侧菜单栏menu-bar固定的的页面
    关于刚才那个全选问题的解决
    关于input[type='checkbox']全选的问题
    用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。
    Vue.js(20)之 封装字母表(是这个名字吗0.0)
    Vue.js之calendar组件
    书:构造器模式
  • 原文地址:https://www.cnblogs.com/fengjian/p/6115478.html
Copyright © 2011-2022 走看看