生产-消费模式的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();
        }
    }

}

 

posted @ 2017-08-24 10:33  街头小瘪三  阅读(171)  评论(0编辑  收藏  举报