新手求解多线程信号灯同步的问题,求大神解决

鱼秋刀 2019-07-05 10:52:36
package CoTest;

/**
* 信号灯
* 借助标志位
* @author yuche
*
*/

public class CoTest2 {
public static void main(String[] args) {
Bread1 bread=new Bread1();
new Producer1(bread).start();
new Consumer1(bread).start();

}

}

class Consumer1 extends Thread{
Bread1 bread;

public Consumer1(Bread1 bread) {
super();
this.bread = bread;
}

@Override
public void run() {
for(int i=1;i<100;++i) {
consume();
}
}

public synchronized void consume(){
if(bread.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费");
bread.flag=!bread.flag;
this.notifyAll();
}

}

class Producer1 extends Thread{
Bread1 bread;

public Producer1(Bread1 bread) {
super();
this.bread = bread;
}

@Override
public void run() {
for(int i=1;i<100;++i) {
produce();
}
}

public synchronized void produce(){
if(!bread.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产");
bread.flag=!bread.flag;
this.notifyAll();
}
}

//资源
class Bread1{
//为T表示面包在生产,为F表示可以消费了
boolean flag;
public Bread1() {
flag=true;
}
}


资源为Bread1,run里面有循环,编译运行就出一次结果,后面的就不运行了

...全文
192 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
鱼秋刀 2019-07-09
  • 打赏
  • 举报
回复
引用 3 楼 qq_39936465 的回复:
[quote=引用 楼主 YuChengwei_ 的回复:] 资源为Bread1,run里面有循环,编译运行就出一次结果,后面的就不运行了
你加的锁在2个线程中,没有交点不会相互唤醒。请把,同步方法放入Bread类中

public class CoTest2 {
	public static void main(String[] args) {
		Bread1 bread = new Bread1();
		new Producer1(bread).start();
		new Consumer1(bread).start();

	}

}

class Consumer1 extends Thread {
	Bread1 bread;

	public Consumer1(Bread1 bread) {
		super();
		this.bread = bread;
	}

	@Override
	public void run() {
		for (int i = 1; i < 100; ++i) {
			bread.consume();
		}
	}

	

}

class Producer1 extends Thread {
	Bread1 bread;

	public Producer1(Bread1 bread) {
		super();
		this.bread = bread;
	}

	@Override
	public void run() {
		for (int i = 1; i < 100; ++i) {
			bread.produce();
		}
	}

	
}

//资源
class Bread1 {
	// 为T表示面包在生产,为F表示可以消费了
	boolean flag;

	public Bread1() {
		flag = true;
	}
	public synchronized void consume() {
		if (flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("消费");
		flag = !flag;
		this.notifyAll();
	}
	public synchronized void produce() {
		if (!flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("生产");
		flag = !flag;
		this.notifyAll();
	}
}


[/quote]明白了,感谢大神
qq_39936465 2019-07-08
  • 打赏
  • 举报
回复
引用 楼主 YuChengwei_ 的回复:
资源为Bread1,run里面有循环,编译运行就出一次结果,后面的就不运行了
你加的锁在2个线程中,没有交点不会相互唤醒。请把,同步方法放入Bread类中

public class CoTest2 {
	public static void main(String[] args) {
		Bread1 bread = new Bread1();
		new Producer1(bread).start();
		new Consumer1(bread).start();

	}

}

class Consumer1 extends Thread {
	Bread1 bread;

	public Consumer1(Bread1 bread) {
		super();
		this.bread = bread;
	}

	@Override
	public void run() {
		for (int i = 1; i < 100; ++i) {
			bread.consume();
		}
	}

	

}

class Producer1 extends Thread {
	Bread1 bread;

	public Producer1(Bread1 bread) {
		super();
		this.bread = bread;
	}

	@Override
	public void run() {
		for (int i = 1; i < 100; ++i) {
			bread.produce();
		}
	}

	
}

//资源
class Bread1 {
	// 为T表示面包在生产,为F表示可以消费了
	boolean flag;

	public Bread1() {
		flag = true;
	}
	public synchronized void consume() {
		if (flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("消费");
		flag = !flag;
		this.notifyAll();
	}
	public synchronized void produce() {
		if (!flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("生产");
		flag = !flag;
		this.notifyAll();
	}
}


顾工讲码 2019-07-06
  • 打赏
  • 举报
回复
你是要写生产者和消费者?
鱼秋刀 2019-07-05
  • 打赏
  • 举报
回复
求大神帮帮忙

62,628

社区成员

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

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