阿里巴巴java研发2015实习笔试题--生产者消费者并发线程安全
2015-04-03 13:41
375 查看
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();
}
}
}
}
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();
}
}
}
}
相关文章推荐
- 阿里巴巴2015秋季笔试研发工程师JAVA
- Java线程:并发协作-生产者消费者模型
- Java并发编程 生产者消费者模式
- Java线程:并发协作-生产者消费者模型
- 求两个字符串公共子序列的最大长度(参考阿里巴巴2015研发笔试)(简单)
- 阿里巴巴2014校园招聘研发笔试一道JAVA附加题分析(update)
- Java线程:并发协作-生产者消费者模型
- 【Java并发编程】之十三:生产者—消费者模型(含代码)
- Java线程:并发协作-生产者消费者模型
- Java线程:并发协作-生产者消费者模型
- 2015阿里巴巴校招杭州java研发面经(初面)
- Java线程:并发协作-生产者消费者模型
- 关于网宿厦门研发中心笔试的一道PV操作题:利用java中的多线程实现生产者与消费者的同步问题
- Java线程:并发协作-生产者消费者模型
- Java线程:并发协作-生产者消费者模型
- Java线程:并发协作-生产者消费者模型
- Java线程:并发协作-生产者消费者模型
- Java线程:并发协作-生产者消费者模型 转自:http://lavasoft.blog.51cto.com/62575/221932
- Java线程:并发协作-生产者消费者模型
- Java 线程与并发研究系列五(生产者与消费者)