zoukankan      html  css  js  c++  java
  • java 多线程四

    java 多线程一

    java 多线程二

    java 多线程三

    java 多线程四

    一个生产者,消费者的例子:

    import java.util.Stack;
    
    /**
     * Created by root on 17-10-1.
     */
    public class Test5 {
    
        //商品:APPLE
        class Apple {
            private int id;
    
            public Apple(int id) {
                this.id = id;
            }
    
            @Override
            public String toString() {
                return "Apple{" +
                        "id=" + id +
                        '}';
            }
        }
    
        //存放商品的仓库
        class Repositorie {
            Stack<Apple> capacity = new Stack<>();
            int MAXSIZE = 10;
    
            public synchronized void putApple(Apple apple) throws InterruptedException {
                if (capacity.size() < MAXSIZE) {
                    capacity.push(apple);
                    System.out.println(Thread.currentThread().getName()+"生产Apple>>>" + apple.toString());
                    notify();
                } else {
                    wait();
                }
            }
    
            public synchronized void getApple() throws InterruptedException {
                if (capacity.size() > 0) {
                    System.out.println("      "+Thread.currentThread().getName()+"消费Apple<<<" + capacity.pop().toString());
                    notify();
                } else {
                    wait();
                }
            }
    
            public void close(){
                while (true){
                    //卖完了就打烊
                    if (capacity.size()==0){
                        System.exit(0);
                    }
    
                }
    
            }
        }
    
    
        //生产者
        class Producer implements Runnable {
            int NUM=20;//一共生产20个商品
            private Repositorie repositorie;
    
            public Producer(Repositorie repositorie) {
                this.repositorie = repositorie;
            }
    
            @Override
            public void run() {
    
                for (; NUM>0; ) {
                    try {
                        repositorie.putApple(new Apple(NUM--));
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                repositorie.close();
            }
        }
    
        //消费者
        class Consume implements Runnable {
    
            private Repositorie repositorie;
    
            public Consume(Repositorie repositorie) {
                this.repositorie = repositorie;
            }
    
            @Override
            public void run() {
                //不知道有多少商品
                for (;true;) {
                    try {
                        repositorie.getApple();
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
        }
    
    
        public static void main(String[] args) {
            Test5 test5 = new Test5();
            Repositorie repositorie = test5.new Repositorie();
            Producer producer = test5.new Producer(repositorie);
            Consume consume = test5.new Consume(repositorie);
            Thread p1 = new Thread(producer,"工人1");
            Thread p2 = new Thread(producer,"工人2");
            Thread c1 = new Thread(consume,"客户1");
            Thread c2 = new Thread(consume,"客户2");
            p1.start();
            p2.start();
            c1.start();
            c2.start();
        }
    }
    

    结果:

    工人1生产Apple>>>Apple{id=20}
    工人2生产Apple>>>Apple{id=19}
          客户2消费Apple<<<Apple{id=19}
          客户1消费Apple<<<Apple{id=20}
    工人1生产Apple>>>Apple{id=18}
    工人2生产Apple>>>Apple{id=17}
    工人1生产Apple>>>Apple{id=16}
    工人2生产Apple>>>Apple{id=15}
    工人1生产Apple>>>Apple{id=14}
          客户1消费Apple<<<Apple{id=14}
          客户2消费Apple<<<Apple{id=15}
    工人2生产Apple>>>Apple{id=13}
    工人1生产Apple>>>Apple{id=12}
    工人2生产Apple>>>Apple{id=11}
    工人1生产Apple>>>Apple{id=10}
    工人2生产Apple>>>Apple{id=9}
    工人1生产Apple>>>Apple{id=8}
          客户1消费Apple<<<Apple{id=8}
          客户2消费Apple<<<Apple{id=9}
    工人2生产Apple>>>Apple{id=7}
    工人1生产Apple>>>Apple{id=6}
    工人2生产Apple>>>Apple{id=5}
          客户1消费Apple<<<Apple{id=5}
          客户2消费Apple<<<Apple{id=6}
    工人1生产Apple>>>Apple{id=2}
    工人2生产Apple>>>Apple{id=1}
          客户1消费Apple<<<Apple{id=1}
          客户2消费Apple<<<Apple{id=2}
          客户1消费Apple<<<Apple{id=7}
          客户2消费Apple<<<Apple{id=10}
          客户1消费Apple<<<Apple{id=11}
          客户2消费Apple<<<Apple{id=12}
          客户1消费Apple<<<Apple{id=13}
          客户2消费Apple<<<Apple{id=16}
          客户1消费Apple<<<Apple{id=17}
          客户2消费Apple<<<Apple{id=18}
    
  • 相关阅读:
    东南亚“美团” Grab 的搜索索引优化之法
    linux中init.d文件夹的说明
    缓存踩踏:Facebook 史上最严重的宕机事件分析
    burp suite
    千万实例可观测采集器iLogtail
    grep 匹配多个关键字
    127.0.0.1和0.0.0.0
    Python 爬虫进阶必备 | 某常见 cookie 加密算法逻辑分析 (加速乐 jsl) https://mp.weixin.qq.com/s/fKuPs2b3MvOi8y4hPVbgNA
    百度商业大规模高性能全息日志检索技术揭秘
    ECB加密 CBC
  • 原文地址:https://www.cnblogs.com/lanqie/p/7617644.html
Copyright © 2011-2022 走看看