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() 方法 的使用。

各位看看有啥问题???谢谢!!!
...全文
266 6 打赏 收藏 转发到动态 举报
写回复
用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) {
            }
        }
内容概要:本文提出了一种基于“空调-电动汽车”联合虚拟储能的海岛微电网优化调度方法,旨在解决海岛地区可再生能源消纳难与电力供应不稳定的问题。通过充分利用空调负荷的热惰性与电动汽车的灵活充放电能力,构建联合虚拟储能系统,等效替代物理储能装置,实现对风电、光伏等间歇性电源出力波动的平抑。研究建立了融合需求响应机制与多时间尺度调度框架的优化模型,并采用Matlab进行仿真验证。结果表明,该策略能够有效提升微电网对分布式能源的接纳能力,降低系统综合运行成本,增强供电可靠性与能源利用效率。; 适合人群:具备一定电力系统分析、优化建模及Matlab编程基础,从事微电网、综合能源系统、虚拟储能、需求响应等领域研究的科研人员与工程技术人员,特别适合研究生及以上学历的研究者。; 使用场景及目标:①应用于海岛、偏远孤岛等孤立微电网场景,提升可再生能源渗透率与系统韧性;②为高比例新能源接入的配电网提供低成本、高灵活性的虚拟储能解决方案;③实现空调与电动汽车等柔性负荷的协同优化,达成削峰填谷、降低能耗与运行成本的目标; 阅读建议:建议结合Matlab代码深入理解模型构建与求解过程,重点关注虚拟储能建模逻辑、温控负荷动态特性刻画、电动汽车充放电约束处理以及多时间尺度滚动优化的实现机制,可进一步拓展至碳交易、绿证激励等复合政策情景下的优化调度研究。

62,621

社区成员

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

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