线程通信问题,实在不知道错哪儿

会飞的大萝卜 2015-07-30 04:28:20
//经典的生产者与消费者问题,线程的通讯知识点
class product1{//定义一个产品类
String name="apple";
double price;
int number;
boolean flag=false;

}
class producer1 extends Thread{//定义一个生产者类
product1 p;//定义一个产品对象的饮用

public producer1(product1 p) {
// TODO Auto-generated constructor stub
this.p=p;
}

@Override
public void run() {
int number=0;
while(true){
synchronized(p){
if(p.flag=false){
if(number%2==0){
p.name="apple";
p.price=2.5;
}
else{
p.name="blanana";
p.price=1.5;
}
System.out.println("生产的产品:"+p.name+"价格:"+p.price);
System.out.println("生产后的商品数量:"+number);
p.flag=true;
number++;
p.notifyAll();
} else
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class custumer extends Thread{
product1 p;//定义一个产品对象的饮用
public custumer(product1 p) {
// TODO Auto-generated constructor stub
this.p=p;
}

@Override
public void run(){
while(true){
synchronized (p) {
if(p.flag=true)
{

System.out.println("消费: "+p.name+"价格是:"+p.price);

p.flag=false;
p.notifyAll();
} else{
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
}
}
}

public class demo1{
public static void main(String[]args) throws InterruptedException{
product1 p=new product1();
producer1 thread1=new producer1(p);
custumer thread2=new custumer(p);
thread1.start();
thread2.start();

}



}











...全文
122 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
peter_xizi 2015-08-06
  • 打赏
  • 举报
回复
结贴
迷林 2015-08-04
  • 打赏
  • 举报
回复
引用 3 楼 qq_15310795 的回复:
@peter_xizi 刚才百度了一下,一直没发现还有这种安全机制在,实在是感谢
楼主可以结贴了
会飞的大萝卜 2015-08-03
  • 打赏
  • 举报
回复
@peter_xizi 刚才百度了一下,一直没发现还有这种安全机制在,实在是感谢
会飞的大萝卜 2015-08-03
  • 打赏
  • 举报
回复
@peter_xizi 感谢
peter_xizi 2015-08-03
  • 打赏
  • 举报
回复
package ProducerAndConsumer; public class demo { private static volatile product p =null; static class product { String name = "apple"; double price=1.0; int number=0; boolean flag = false; } static class producer extends Thread { product p;// 定义一个产品对象的饮用 public producer(product p) { // TODO Auto-generated constructor stub this.p = p; } @Override public void run() { int number = 0; while (true) { synchronized (p) { if (p.flag == false) { if (number % 2 == 0) { p.name = "apple"; p.price = 2.5; } else { p.name = "blanana"; p.price = 1.5; } System.out.println("Produce Product:" + p.name + " --Price:" + p.price); System.out.println("Product Number:" + number); p.flag = true; number++; p.notifyAll(); } else try { p.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } static class custumer extends Thread { product p;// 定义一个产品对象的饮用 public custumer(product p) { this.p = p; } @Override public void run() { while (true) { synchronized (p) { if (p.flag == true) { System.out.println("Consumber: " + p.name + " -- Price:" + p.price); p.flag = false; p.notifyAll(); } else { try { p.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } public static void main(String[] args) throws InterruptedException { p = new product(); producer thread1 = new producer(p); custumer thread2 = new custumer(p); thread1.start(); thread2.start(); } } 把你的代码稍微改造了一下,见红色部分, 1. volatile保证多线程的可见性,2.if条件判断 以上代码已测试通过: Produce Product:apple --Price:2.5 Product Number:28642 Consumber: apple -- Price:2.5 Produce Product:blanana --Price:1.5 Product Number:28643 Consumber: blanana -- Price:1.5 Produce Product:apple --Price:2.5 Product Number:28644 Consumber: apple -- Price:2.5 Produce Product:blanana --Price:1.5 Product Number:28645 Consumber: blanana -- Price:1.5 Produce Product:apple --Price:2.5 Product Number:28646 Consumber: apple -- Price:2.5 Produce Product:blanana --Price:1.5 Product Number:28647 Consumber: blanana -- Price:1.5 ......

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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