zoukankan      html  css  js  c++  java
  • 多线程-生产者消费者(lock同步)

    二、采用Lock锁以及await和signal方法是实现

    import java.io.IOException;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * @author 作者 E-mail:
     * @version 创建时间:2015-10-23 下午04:08:39 类说明
     */
    public class AwaitAndSignal
    {
        public static void main(String[] args) throws IOException
        {
            Person person = new Person();
            new Thread(new Consumer(person), "消费者一").start();
            new Thread(new Consumer(person), "消费者二").start();
            new Thread(new Consumer(person), "消费者三").start();
    
            new Thread(new Producer(person), "生产者一").start();
            new Thread(new Producer(person), "生产者一").start();
            new Thread(new Producer(person), "生产者一").start();
        }
    }
    
    class Producer implements Runnable
    {
        private Person person;
    
        public Producer(Person person)
        {
            this.person = person;
        }
    
        @Override
        public void run()
        {
    
            for (int i = 0; i < 10; i++)
            {
                person.produce();
            }
    
        }
    
    }
    
    class Consumer implements Runnable
    {
    
        private Person person;
    
        public Consumer(Person person)
        {
            this.person = person;
        }
    
        @Override
        public void run()
        {
    
            for (int i = 0; i < 10; i++)
            {
                person.consume();
            }
    
        }
    
    }
    
    class Person
    {
        private int foodNum = 0;
    
        private ReentrantLock lock = new ReentrantLock();
    
        private Condition condition = lock.newCondition();
    
        private final int MAX_NUM = 5;
    
        public void produce()
        {
            lock.lock();
            try
            {
                while (foodNum == MAX_NUM)
                {
                    System.out.println("box is full,size = " + foodNum);
                    condition.await();
                }
                foodNum++;
                System.out.println("produce success foodNum = " + foodNum);
                condition.signalAll();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            } finally
            {
                lock.unlock();
            }
    
        }
    
        public void consume()
        {
            lock.lock();
            try
            {
                while (foodNum == 0)
                {
                    System.out.println("box is empty,size = " + foodNum);
                    condition.await();
                }
                foodNum--;
                System.out.println("consume success foodNum = " + foodNum);
                condition.signalAll();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            } finally
            {
                lock.unlock();
            }
    
        }
    }
    
  • 相关阅读:
    Eclipse添加Android library错误的原因
    The primitive Java types
    TFS怎么查找一个用户提交的所有代码
    easyui tabs
    错误 CS0012 类型“xxx”在未引用的程序集中定义。必须添加对程序集“xxxx xxxxx”的引用
    Sql Server 列转逗号隔开的字符串 和 逆转
    DataTable程序分页 不推荐。
    EF执行存储过程并且返回DataSet
    EF运用成ADO.NET操作返回DataSet或者DataTable
    Sql 查询 表名 视图名称等
  • 原文地址:https://www.cnblogs.com/Andrew520/p/8884003.html
Copyright © 2011-2022 走看看