• 生产-消费模式的synchronized和lock实现(十)


    lock:

    package com.net.thread.lock;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * @author 
     * @Time:2017年8月23日 下午5:06:36
     * @version 1.0
     * @description 多生产/多消费,同步lock锁实现
     */
    public class ReenTrantLockDemo3
    {
        public static void main(String[] args)
        {
            ReadThread t = new ReadThread();
            for(int i = 0; i < 5; i++){
                new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        while(true)
                        {
                            t.printX();
                        }
                    }
                }).start();
                
                new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        while(true)
                        {
                            t.printY();
                        }
                    }
                }).start();
            }
        }
    
        static class ReadThread
        {
            private ReentrantLock lock = new ReentrantLock();
            private Condition condition = lock.newCondition();
            private boolean isFalgX = true;
            
            public void printX()
            {
                try {
                    lock.lock();
                    while(!isFalgX){
                        condition.await();
                    }
                    System.out.println(Thread.currentThread().getName() + "  +++++ " + System.currentTimeMillis());
                    Thread.sleep(1000);
                    isFalgX = false;
                    condition.signalAll();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    lock.unlock();
                }
            }
            
            public void printY()
            {
                try {
                    lock.lock();
                    while(isFalgX){
                        condition.await();
                    }
                    System.out.println(Thread.currentThread().getName() + "  ----- " + System.currentTimeMillis());
                    Thread.sleep(1000);
                    isFalgX = true;
                    condition.signalAll();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    lock.unlock();
                }
            }
        }
    
    }

    2、synchronized方法实现

    package com.net.thread.synchonized;
    
    /**
     * @author 
     * @Time:2017年8月18日 下午4:20:36
     * @version 1.0
     * @description   多生产生产/多消费模式,无限循环
     */
    public class SynMethodDemo3 {
    
        public static void main(String[] args)
        {
            MyThread mt = new MyThread();
            for(int j = 0; j < 20; j++)
            {
                // put线程
                new Thread(new Runnable()
                {
                    public void run()
                    {
                        while(true)
                        {
                            mt.put();
                        }
                    }
                }).start();
    
                // take线程
                new Thread(new Runnable()
                {
                    public void run()
                    {
                        while(true)
                        {
                            mt.take();
                        }
                    }
                }).start();
            }
        }
        
        
        static class MyThread
        {
            private boolean isPut = true;
            public synchronized void put()
            {
                while (!isPut) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 0; i < 2; i++) {
                    System.out.println("put方法 : ++++++++++++++++++++" + i + "  :  " + Thread.currentThread().getName());
                }
                
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                isPut = false;
                this.notifyAll();
            }
    
            public synchronized void take()
            {
                while(isPut)
                {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 0; i < 2; i++) {
                    System.out.println("take方法 :-------------------" + i  + "  :  " + Thread.currentThread().getName());
                }
                
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                isPut = true;
                this.notifyAll();
            }
        }
    
    }
  • 相关阅读:
    cocos2D-X 4.0 build
    Boost Download
    史上最全的CSS hack
    web前端js中ES6的规范写法
    爬虫最终杀手锏 --- PhantomJS 详解(附案例)
    移动端300ms延迟的解决方法
    js动态生成html元素并为元素追加属性
    css中border-sizing属性详解和应用
    纯CSS实现Tab切换
    阻止移动端浏览器点击图片会预览的几种方法
  • 原文地址:https://www.cnblogs.com/chen1-kerr/p/7421747.html
Copyright © 2020-2023  润新知