黑马程序员技术交流社区
标题:
多线程,找错误,求帮助!
[打印本页]
作者:
坚强
时间:
2011-11-14 22:16
标题:
多线程,找错误,求帮助!
我按照视频敲的代码,不知道哪里错了,只会运行3次就停了,求高手指点!
class Resource
{
private String name;
private int count=1;
private boolean flag =false;
public synchronized void set(String name)
{
if(flag)
try{this.wait();}catch(Exception a){}
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
flag=true;
this.notify();
}
public synchronized void out()
{
if(!flag)
try{this.wait();}catch(Exception a){}
System.out.println(Thread.currentThread().getName()+"...消费者...."+this.name);
flag=false;
this.notify();
}
}
//---------------------------------
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
res.set("商品");
}
}
}
//----------------------------------
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res=res;
}
public void run()
{
res.out();
}
}
//------------------------------------
class ProduceConsumerDemo
{
public static void main(String[] args)
{
Resource r =new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
t1.start();
t2.start();
}
}
复制代码
作者:
陈超
时间:
2011-11-14 22:52
本帖最后由 陈超 于 2011-11-14 22:54 编辑
Consumer类中的run()加入while循环就行了,42.class Consumer implements Runnable
43.{
44. private Resource res;
45. Consumer(Resource res)
46. {
47. this.res=res;
48. }
49. public void run()
50. {
51.
res.out();//此处用while(true){res.out();}代替
52. }
53.}
作者:
胡文杰
时间:
2011-11-15 00:20
package poc;
public class Resource {
private String name;
private int count=1;
private boolean flag =true;
public synchronized void set(String name)
{
while(count<=100){
if(!flag)
try{this.wait();}catch(Exception a){}
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
flag=false;
this.notify();
}
}
public synchronized void out()
{
while(count<=100){
if(flag)
try{this.wait();}catch(Exception a){}
System.out.println(Thread.currentThread().getName()+"...消费者...."+this.name);
flag=true;
this.notify();
}
}
}
//---------------------------------
package poc;
public class Producer implements Runnable {
private Resource res;
Producer(Resource res)
{
this.res=res;
}
public void run()
{
res.set("商品");
}
}
//----------------------------------
package poc;
import org.omg.SendingContext.RunTime;
public class Consumer implements Runnable {
private Resource res;
Consumer(Resource res)
{
this.res=res;
}
public void run()
{
res.out();
}
}
//------------------------------------
class ProduceConsumerDemo
{
public static void main(String[] args)
{
Resource r =new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
t1.start();
t2.start();
}
}
给你正确的写法,你的while循环不对!
生产一个商品,消费一个商品,你只消费了一次,没有循环,当生产的线程进入wait()了需要消费来给其唤醒,因为没有循环了,怎么唤醒生产呢?!
作者:
坚强
时间:
2011-11-15 20:30
胡文杰 发表于 2011-11-15 00:20
package poc;
public class Resource {
恩 对 我照你的改好了 谢谢哦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2