黑马程序员技术交流社区
标题:
生产者与消费者问题
[打印本页]
作者:
demown
时间:
2014-4-18 10:46
标题:
生产者与消费者问题
代码如下 程序一直运行就是打印不来结果.
class SCXF {
public static void main(String[] ages){
Rexource r =new Rexource();
cont pw = new cont(r);
Producel1 pq=new Producel1(r);
Thread t1=new Thread(pq);
Thread t2=new Thread(pw);
t1.start();
t2.start();
}
}
class Rexource{
private String name;
private int count =1;
private boolean flag = false;
public synchronized void set(String name){
while(flag){try{wait();}catch(Exception e){}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
this.notifyAll();}}
public synchronized void get_1(String name){
while(!flag){try{wait();}catch(Exception e){}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
flag = true;
this.notifyAll();
}
}}
class Producel1 implements Runnable
{
private Rexource res ;
Producel1 (Rexource res){
this.res = res;
}
public void run() {
while(true){
res.set("++商品++");
}
} }
class cont implements Runnable
{
private Rexource res ;
cont (Rexource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.get_1("++消费++");}
}
}
作者:
sanguodouble1
时间:
2014-4-18 11:03
兄弟,你这个代码太烂了,类名也不大写,格式也不排好,注释也没有
我先看一下,看懂了再说
作者:
sanguodouble1
时间:
2014-4-18 11:21
本帖最后由 sanguodouble1 于 2014-4-18 11:23 编辑
兄弟,你这个逻辑这真没法说,
你开了一个Produce11的线程,又开了一个Cont线程,对吧?
先说Producel1线程吧,执行的get_1方法,对吧,问题你进去后就wait了,那就看Cont线程了
但你Cont线程运行的时候flag又是false,也就是进不去set方法,
一个在一直wait,一个在run里面死循环,那你说无语不无语?
哎,这代码,这问题,估计技术分也拿不到,还看得我眼花
作者:
曹冬明
时间:
2014-4-18 12:18
public synchronized void set(String name)
{
while(flag)
{try{wait();}catch(Exception e){}}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
this.notifyAll();
}
public synchronized void get_1(String name)
{
while(!flag)
{try{wait();}catch(Exception e){}}//这里加个“}”
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
flag = false;//这里改成false 否则你的线程1别想执行
this.notifyAll();
}
}//这里减个“}”
复制代码
朋友,while(flag)判断标记是否使本线程等待,你却吧所有的代码都写到while代码块里去了,flag默认为false,你的线程1,一致性,while不满足相当于没有代码,线程2while满足了,执行第一行代码线程就等待,第一个根本就不能执行,第二个却一直等待,你应该把代码改改!我帮你改了下,不过这样即使运行了,我不知道你的目的是什么,两个线程运行的是相同的代码!还有朋友,你要注意下代码书写,还有不要太粗心了,这个错误太低级了
作者:
demown
时间:
2014-4-18 21:59
曹冬明 发表于 2014-4-18 12:18
朋友,while(flag)判断标记是否使本线程等待,你却吧所有的代码都写到while代码块里去了,flag默认为false ...
非常感谢
作者:
湛添友
时间:
2014-4-19 00:27
class SCXF {
public static void main(String[] ages){
Rexource r =new Rexource();
cont pw = new cont(r);
Producel1 pq=new Producel1(r);
Thread t1=new Thread(pq);
Thread t2=new Thread(pw);
t1.start();
t2.start();
}
}
class Rexource
{
private String name;
private int count =1;
private boolean flag = false;
public synchronized void set(String name)
{
while(flag)
{
try
{
wait();
}
catch(Exception e)
{}
}//修改的地方 while只控制等待
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
this.notifyAll();
}
public synchronized void get_1(String name){
while(!flag){try{wait();}catch(Exception e){}}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
flag = false;//修改的地方
this.notifyAll();
}}
class Producel1 implements Runnable
{
private Rexource res ;
Producel1 (Rexource res){
this.res = res;
}
public void run() {
while(true){
res.set("++商品++");
}
} }
class cont implements Runnable
{
private Rexource res ;
cont (Rexource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.get_1("++消费++");}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2