zoukankan      html  css  js  c++  java
  • Java基础多线程间通讯之多生产者、多消费者模式示例:

    class ProducerConsumerDemo
    {
        public static void main(String[] args)
        {
            Product product = new Product();
            
            new Thread(new Producer(product)).start();
            new Thread(new Consumer(product)).start();
            new Thread(new Producer(product)).start();
            new Thread(new Consumer(product)).start();
            new Thread(new Producer(product)).start();
            new Thread(new Consumer(product)).start();
            new Thread(new Producer(product)).start();
            new Thread(new Consumer(product)).start();
        }
    }

    class Product
    {
        private int count;
        private boolean flag;

        public synchronized void create()
        {
            
            while(this.flag)
            {
                try
                {
                    this.wait();
                }
                catch(Exception e)
                {
                    System.err.println(e.getMessage());
                }
            }
                
            this.count++;    
            System.out.println(Thread.currentThread().getName()+" create -- :"+this.count);
            this.flag = true;
            this.notifyAll();
                
            try
            {
                Thread.sleep(300);
            }
            catch(Exception e)
            {
                System.err.println(e.getMessage());
            }
            
        }
        
        public synchronized void get()
        {
            while(!this.flag)
            {
                try
                {
                    this.wait();
                }
                catch(Exception e)
                {
                    System.err.println(e.getMessage());
                }
            }
                
            System.out.println(Thread.currentThread().getName()+" sale:"+this.count);
            this.flag = false;
            this.notifyAll();
                
            try
            {
                Thread.sleep(600);
            }
            catch(Exception e)
            {
                System.err.println(e.getMessage());
            }
            
        }
    }

    class Producer implements Runnable
    {
        private Product product;
        
        public Producer(Product product)
        {
            this.product = product;
        }
        
        public void run()
        {
            while(true)
            {
                product.create();
            }
        }
    }

    class Consumer implements Runnable
    {
        private Product product;
        
        public Consumer(Product product)
        {
            this.product = product;
        }
        
        public void run()
        {
            while(true)
            {
                product.get();
            }
        }
    }
  • 相关阅读:
    CE6内核启动过程新角度
    Eboot 中给nandflash分区实现
    Feedsky 上 csdn blog 订阅数排名 (zz)
    c# 枚举 enum 类型
    64bit系统下操作注册表的注意事项 ZZ
    良方治奇病,Visual Studio正则替换大法 (转)
    关于23种设计模式的有趣见解
    NHibernate快速指南(一)
    网站、数据库的衍变之路(二) (转)
    网站、数据库的衍变之路(一) (转)
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860936.html
Copyright © 2011-2022 走看看