zoukankan      html  css  js  c++  java
  • 阿里巴巴java研发2015实习笔试题--生产者消费者并发线程安全

    import java.util.ArrayList;
    import java.util.List;
    /**
    * 箱子最多装5个苹果。一个人往里放,一个人往外拿。苹果无限。
    * @author Administrator
    */
    public class test01 {
    public static void main(String[] args) {
    // 共享资源
    Production pro = new Production();
    Custom custom = new Custom(pro);
    Producer producer = new Producer(pro);
    new Thread(producer).start();
    new Thread(custom).start();
    }
    }

    // 资源
    class Production {
    private String src;
    private List<String> box = new ArrayList<String>();// 箱子
    private boolean flag1 = false;// 生产开始
    private boolean flag2 = true;// 消费等待

    public synchronized void product(String src) throws Exception {
    if (flag1) {// true 生产等待
    this.wait();
    }
    // 开始生产
    Thread.sleep(300);
    // 生产完毕
    this.src = src;
    // 放入箱子
    box.add(src);
    System.out.println("放入了----------->" + src);
    System.out.println("箱子里有" + box.size() + "个苹果");
    // 开始消费
    this.flag2 = false;
    // 通知消费
    this.notify();
    if (box.size() >= 5) {
    // 停止生产
    this.flag1 = true;
    }
    }

    public synchronized void custom() throws Exception {
    if (flag2) {// true 消费等待
    this.wait();
    }
    // 开始消费
    Thread.sleep(200);
    src = box.get(0);
    System.out.println("拿走了--->" + src);
    box.remove(0);
    // 消费完毕
    this.flag1 = false;
    // 通知生产
    this.notifyAll();
    if (box.size() <= 0) {
    // 停止消费
    this.flag2 = true;
    }
    }
    }

    // 生产
    class Producer implements Runnable {
    Production p;

    public Producer(Production p) {
    this.p = p;
    }

    public void run() {
    int i = 1;
    while(true) {
    try {
    p.product("第" + i + "个苹果");
    i++;
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    }

    // 消费
    class Custom implements Runnable {
    Production p;

    public Custom(Production p) {
    this.p = p;
    }

    public void run() {
    while(true) {
    try {
    p.custom();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }

  • 相关阅读:
    HihoCoder1371
    Intern Day23
    腾讯移动客户端开发暑期实习一面笔试
    C++
    朱丹为什么是文艺青年
    金数据一个不错的调查平台
    单反手动对焦M档,AV,TV,P,A,A-DEP
    chrome不支持字体12px
    火狐解决字体模糊
    《程序员的自我修养》阅读笔记(四):
  • 原文地址:https://www.cnblogs.com/king-/p/4389852.html
Copyright © 2011-2022 走看看