您的位置:首页 > 编程语言 > Java开发

java使用多线程实现生产者消费者问题

2015-12-20 22:37 841 查看
       本例中我们用模拟吃苹果来实现生产者消费者问题:

       模拟吃苹果的实例

  一个家庭有三个孩子,爸爸妈妈不断削苹果往盘子里面放,老大、老二、老三不断从盘子里面取苹果吃。

盘子的大小有限,最多只能放5个苹果,并且爸妈不能同时往盘子里面放苹果,妈妈具有优先权。

三个孩子取苹果时,盘子不能为空,三人不能同时取,老三优先权最高,老大最低。

老大吃的最快,取的频率最高,老二次之 ,老三再次之. 爸妈一共只能放20个苹果

程序设计了四个类:Test类、Productor类、Consumer类和Dish类。其中Dish类是中间类,包含了共享数据区和放苹果、取苹果的方法。

下面先给大家放上初步的代码,基本功能实现,但是耦合度比较高,不利于后期维护虽然咱这是个小例子但是也要认真去做;

package com.caoxing.apple;

public class Test {
public static void main(String[] args) {

Dish dish = new Dish();
Thread mother = new Thread(new Productor(3, dish), "妈妈");
Thread father = new Thread(new Productor(3, dish), "爸爸");
Thread oneSon = new Thread(new Consumer(1, dish), "老大");
Thread twoSon = new Thread(new Consumer(2, dish), "老二");
Thread threeSon = new Thread(new Consumer(3, dish), "老三");
mother.setPriority(8);
father.setPriority(7);
oneSon.setPriority(6);
twoSon.setPriority(8);
threeSon.setPriority(9);
mother.start();
father.start();
oneSon.start();
twoSon.start();
threeSon.start();

}

}
package com.caoxing.apple;

/**
* 消费者
*
* @author see
*
*/
public class Consumer implements Runnable {

private int time;// 睡眠时间
private Dish consumerDish;

public Consumer(int time, Dish dish) {

this.time = time;
this.consumerDish = dish;

}

public void run() {
while (true) {

consumerDish.EatApple();

try {
Thread.sleep(time * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Dish.sum == 20 && Dish.AppleCount == 0) {
break;
}

}
System.out.println(Thread.currentThread().getName() + " 儿子们吃完了~~");

}

}


package com.caoxing.apple;

/**
* 生产者
*
* @author see
*
*/
public class Productor implements Runnable {

private int time;// 生产者睡眠时间
private Dish productorDish;

public Productor(int time, Dish dish) {

this.time = time;
this.productorDish = dish;
}

public void run() {
while (Dish.sum < 20) {

productorDish.CutApple();
try {
Thread.sleep(time * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

System.out.println(Thread.currentThread().getName() + " 削不动了~~");
System.out.println(Thread.currentThread().getName() + " 一共削了"
+ Dish.sum + "个");

}

}


package com.caoxing.apple;

public class Dish {

public static int AppleCount = 0;
public static int sum = 0;

public Dish() {
}

/**
* 削苹果
*/
public void CutApple() {
synchronized (this) {
if (Dish.AppleCount == 5) {
try {
System.out.println(Thread.currentThread().getName()
+ " 盘子满了~~");
this.wait();// 线程等待

} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (Dish.AppleCount != 5 && Dish.sum != 20) {
Dish.AppleCount++;
Dish.sum++;
System.out.println(Thread.currentThread().getName()
+ " 削了一个苹果+");
this.notify();// 通知线程启动
}
}

}

/**
* 吃苹果
*/
public void EatApple() {

synchronized (this) {
if (Dish.AppleCount == 0 && Dish.sum != 20) {
try {

System.out.println(Thread.currentThread().getName()
+ " 盘中暂时没有苹果了~~");
this.wait();

} catch (InterruptedException e) {
e.printStackTrace();

}
} else if (Dish.AppleCount == 0 && Dish.sum == 20) {
System.out.println("苹果吃完了");
} else if (Dish.AppleCount > 0) {
AppleCount--;
System.out.println(Thread.currentThread().getName()
+ " 吃了一个苹果-");
this.notify();
}
}

}
}
这是初步的实现下面我们再送上修改之后的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: