• Condition


    Condition进一步封装了Object的wait  notify等方法

    public class Bussiness {
        
        private boolean bool = true;
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();
        
        public void main(int loop) throws InterruptedException {
            lock.lock();
            try{
                while(bool){
                    //wait()方法
                    condition.await();
                }
                for(int i = 0;i < 100;i++){
                    System.out.println("main thread seq of "+i+", loop of "+loop);
                }
                bool = true;
                //notify()方法
                condition.signal();
            }finally{
                lock.unlock();
            }
        }
    
        public void sub(int loop) throws InterruptedException{
            lock.lock();
            try{
                while(!bool){
                    condition.await();
                }
                for(int i  =0;i<10;i++){
                    System.out.println("sub thread seq of "+i+", loop of "+ loop);
                }
                bool = false;
                condition.signal();
            }finally{
                lock.unlock();
            }     
        }
    }
    public class ThreadTest {
        public static void main(String[] args) {
            final Bussiness bussiness = new Bussiness();
            new Thread(new Runnable(){
                public void run() {
                    threadExecute(bussiness, "sub");
                }
            }).start();
            threadExecute(bussiness, "main");
        }
    
        public static void threadExecute(Bussiness bussiness,String threadName){
            for(int i = 0; i < 100;i++){
                try{
                    if("main".equals(threadName)){
                        bussiness.main(i);
                    }else{
                        bussiness.sub(i);
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    Condition可以为多个线程建立不同的condition

    /**
     * 多个condition的好处:
     * @author hp
     *
     */
    public 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{
            lock.lock();
            try{
                while(count == items.length){
                    notFull.wait();
                }
                items[putptr] = x;
                if(++putptr == items.length)    //如果写索引到了队列的最后一个位置,那么就置为0
                    putptr=0;
                ++count;
                notEmpty.signal();
            }finally{
                lock.unlock();
            }
        }
        
        
        public Object take() throws InterruptedException{
            lock.lock();
            try{
                //如果队列中为空,则唤醒写线程
                while(count == 0){
                    notEmpty.await();
                }
                Object x = items[takeptr];
                if(++takeptr == items.length)
                    takeptr = 0;
                --count;
                notFull.signal();
                return x;
            }finally{
                lock.unlock();
            }
            
        }
        
    }
  • 相关阅读:
    新建MDK工程
    winform如何打开电脑自带小软件
    Winform界面适应不同分辨率
    c#如何为pictureBox控件写单击事件
    winform如何获取记事本内容显示在listBox控件中
    winform如何单击X按钮弹出对话框
    win10系统如何使用自带的备份功能
    模拟iis账号权限
    (转发)在ASP.NET MVC中以post方式传递数组参数的示例
    .net开发windows服务小结 (转发)
  • 原文地址:https://www.cnblogs.com/lfdingye/p/7445247.html
Copyright © 2020-2023  润新知