zoukankan      html  css  js  c++  java
  • 线程经典 生产者消费者 两个完全不…

    package sell_ticket;

    public class ThreadTicket {

          public static void main(String[] args) {
               
             
              
                MyThread m = new MyThread();        
                Thread t1 = new Thread(m);
                Thread t2 = new Thread(m);
                Thread t3 = new Thread(m);
                t1.start();
                t2.start();
                t3.start();
              
          
          
    }

    class MyThread implements Runnable{
        private int ticket=100;
        
        
        public void run(){
            while(true){
                
                  try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                  }
                //用synchronized包起来,形成同步代码块,但后来发现用不用其实一样
                synchronized(this){
                    if(ticket>0){
                        System.out.println(Thread.currentThread().getName()+"出售了"+ticket);            
                        ticket--;
                    }
                }
                
            }
        }
        
        
    }





    第二种,采用堆栈的方式:

     class Producer implements Runnable { 
     
        private String producerName = null;
     
        private StoreHouse storeHouse = null;
     
        public Producer(String producerName, StoreHouse storeHouse) {
            this.producerName = producerName;
            this.storeHouse = storeHouse;
        }
     
        public void setProducerName(String producerName) {
            this.producerName = producerName;
        }
     
        public String getProducerName() {
            return producerName;
        }
        
        public void produceProduct() {
            int i = 0;
            while (true) {
                i++;
                Product pro = new Product(i);
                storeHouse.push(pro);
                System.out.println(getProducerName() + " 生产了 " + pro);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
     
        public void run() {
            produceProduct();
        }
     
    }

    class Consumer implements Runnable {
        
        private String consumerName = null;
        
        private StoreHouse storeHouse = null;
     
        public Consumer(String consumerName, StoreHouse storeHouse) {
            this.consumerName = consumerName;
            this.storeHouse = storeHouse;
        }
        
        public void setConsumerName(String consumerName) {
            this.consumerName = consumerName;
        }
        
        public String getConsumerName() {
            return consumerName;
        }
        
        public void consumerProduct() {
            while (true) {
                System.out.println(getConsumerName() + " 消费了 " + storeHouse.pop());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
     
        public void run() {
            consumerProduct();
        }
     
    }


     
    class Product {
     
        private int productId = 0;
        
        public Product(int productId) {
            this.productId = productId;
        }
     
        public int getProductId() {
            return productId;
        }
     
        public String toString() {
            return Integer.toString(productId);
        }
    }

    class StoreHouse {
     
        private int base = 0;
     
        private int top = 0;
     
        private Product[] products = new Product[10];
     
        public synchronized void push(Product product) {
            while (top == products.length) {
                notify();
                try {
                    System.out.println("仓库已满,正等待消费...");
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("stop push product because other reasons");

                }
            }
            products[top] = product;
            top++;
        }
     
        public synchronized Product pop() {
            Product pro = null;
            while (top == base) {
                notify();
                try {
                    System.out.println("仓库已空,正等待生产...");
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("stop push product because other reasons");

                }
            }
            top--;
            pro = products[top];
            products[top] = null;
            return pro;
        }
    }

    public class TestProducer {
        public static void main(String[] args) {
            StoreHouse storeHouse = new StoreHouse();
            Producer producer = new Producer("生产者", storeHouse);
            Consumer comsumer = new Consumer("消费者", storeHouse);
            Thread t1 = new Thread(producer);
            Thread t2 = new Thread(comsumer);
            t1.start();
            t2.start();
        }
    }
  • 相关阅读:
    jqgrid 设置单元格编辑/不可编辑
    [坑]c#中double转字符串精度丢失问题记录
    twemproxy接收流程探索——剖析twemproxy代码正编
    twemproxy代码框架概述——剖析twemproxy代码前编
    twemproxy架构分析——剖析twemproxy代码前编
    剖析twemproxy前言
    mysql交互协议解析——mysql包基础数据、mysql包基本格式
    有关binlog的那点事(三)(mysql5.7.13)
    有关binlog的那点事(二)(mysql5.7.13)
    slave IO流程之二:注册slave请求和dump请求
  • 原文地址:https://www.cnblogs.com/xiaowangba/p/6314396.html
Copyright © 2011-2022 走看看