62,614
社区成员
发帖
与我相关
我的任务
分享
Bread.java
public class Bread {
private int counts;
private static int flag;
public Bread(int counts) {
this.counts = counts;
Bread.flag = 0;// 没有面包
}
public synchronized void set() {
if (flag != 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println("开始生产");
for (int i = 1; i <= counts; ++i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(i + "个");
flag++;
}
System.out.println("生产完毕");
notifyAll();
}
public synchronized void get() {
if (flag == 0) {
System.out.println("启动消费");
System.out.println("还没有生产出面包,等待中...");
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println("开始消费");
for (int i = counts; i >= 1; --i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(i + "个");
flag--;
}
System.out.println("消费完毕。");
notify();
}
}
Customor.java
public class Customor implements Runnable{
Bread bre = new Bread(5);
public void run() {
bre.get();
}
}
Productor.java
public class Prodctor implements Runnable {
Bread bre = new Bread(5);
public void run() {
bre.set();
}
}
BreadMain.java
public class BreadMain {
public static void main(String[] args){
Prodctor pro = new Prodctor();
Customor cut = new Customor();
Thread proct = new Thread(pro);
Thread custom = new Thread(cut);
custom.start();
proct.start();
}
}
public class ProducerConsumer {
public static void main(String[] args)
{
Bag b = new Bag();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
new Thread(p).start();
new Thread(c).start();
}
}
class Bread
{
private int id;
public Bread(int id)
{
this.id = id;
}
public String toString()
{
return "I am Bread : " + id;
}
}
class Bag
{
Bread[] bread = new Bread[5];
private int index = 0;
public synchronized void set(Bread b)
{
while(index == bread.length)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.notify();
bread[index] = b;
index ++;
}
public synchronized Bread get()
{
while(index == 0)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.notify();
index --;
return bread[index];
}
}
class Producer implements Runnable
{
Bag bag;
public Producer(Bag bag)
{
this.bag = bag;
}
public void run()
{
for(int i=1; i<=20; i++)
{
Bread b = new Bread(i);
bag.set(b);
System.out.println("Producer : " + b);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
Bag bag;
public Consumer(Bag bag)
{
this.bag = bag;
}
public void run()
{
for(int i=1; i<=20; i++)
{
Bread b = bag.get();
System.out.println("Consumer : " + b);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
参考参考。。
for (int i = counts; i >= 1; --i) {
这个条件并不是保证一次生产5个面包。
一次性要取出5个面包的话,要保证面包数>=5才可以执行,所以等待条件应该是,flag<5,下面的代码有问题
if (flag == 0) {...}
同样set方法也有问题,应该保证能够容纳5个面包。
if (flag != 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
改为
while(flag != 0) {
try {
wait();
} catch (InterruptedException e) {
}
}