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();
        }
    }

    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.notify();
                
            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.notify();
                
            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();
            }
        }
    }
  • 相关阅读:
    2018.12.17-dtoj-1174-出现或反转后出现在每个字符串中的最长子串
    2018.12.17-dtoj-1173-每个字符串至少出现两次且不重叠的最长子串
    2018.12.17-dtoj-1171-长度不小于k的公共子串的个数
    2018.12.17-dtoj-1170-最长公共子串
    2018.12.17-dtoj-1168-连续重复子串
    欧拉四面体公式
    超级密码 hdu1226 bfs
    糖果大战 hdu1204
    Find The Multiple (poj1426 一个好的做法)
    Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860931.html
Copyright © 2011-2022 走看看