码迷,mamicode.com
首页 > 编程语言 > 详细

阿里巴巴java研发2015实习笔试题--生产者消费者并发线程安全

时间:2015-04-03 14:59:34      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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

阿里巴巴java研发2015实习笔试题--生产者消费者并发线程安全

标签:

原文地址:http://www.cnblogs.com/king-/p/4389852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!