zoukankan      html  css  js  c++  java
  • 生产-消费模式的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();
            }
        }
    
    }
  • 相关阅读:
    吴裕雄--天生自然 Zookeeper学习笔记--Zookeeper 权限控制 ACL
    【机器学习】机器学习基础
    【QT】利用pyqt5实现简单界面
    【Mathtype】安装Mathtype后,word无法粘贴的问题
    【优化方法】牛顿法
    博客样式设置
    2018.8.28 练习赛
    2018.8.27 练习赛
    2018.8.26 练习赛
    2018.8.25 练习赛
  • 原文地址:https://www.cnblogs.com/chen1-kerr/p/7421747.html
Copyright © 2011-2022 走看看