java同步问题

野树林 2012-12-24 11:00:02


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


程序 要求:
(1) 一次 生产 5个面包,一次 消费 5个面包。
(2) 注意 同步 机制 , wait()方法 ,no tify() 方法 的使用。

各位看看有啥问题???谢谢!!!
...全文
194 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
eviljordan 2012-12-25
  • 打赏
  • 举报
回复

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();
			}
		}
	}
}
参考参考。。
ghchen 2012-12-25
  • 打赏
  • 举报
回复
不错,学习一下了
je_gs 2012-12-25
  • 打赏
  • 举报
回复
安特矮油 2012-12-25
  • 打赏
  • 举报
回复
你把5个封装为一个对象,这样一次性交给消费者。
stalendp 2012-12-24
  • 打赏
  • 举报
回复
不好意思,要求看错。 一次 生产 5个面包,一次 消费 5个面包。得修改循环条件。

for (int i = counts; i >= 1; --i) {
这个条件并不是保证一次生产5个面包。 一次性要取出5个面包的话,要保证面包数>=5才可以执行,所以等待条件应该是,flag<5,下面的代码有问题
if (flag == 0) {...}
同样set方法也有问题,应该保证能够容纳5个面包。
stalendp 2012-12-24
  • 打赏
  • 举报
回复
你应该把单一的get和set写成线程安全的(换句话,就是保证生产和消费一个产品的过程为原子性的);而你的程序的原子性太大了,保证了一次性生产或者消费掉所有的产品。 还有多线程中的条件判断因该写成while形式的,把:

if (flag != 0) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
改为

while(flag != 0) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧