A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© demown 中级黑马   /  2014-4-18 10:46  /  1351 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

代码如下  程序一直运行就是打印不来结果.
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("++消费++");}
}
}

点评

注意代码排版  发表于 2014-4-18 12:51

6 个回复

倒序浏览
兄弟,你这个代码太烂了,类名也不大写,格式也不排好,注释也没有
我先看一下,看懂了再说
回复 使用道具 举报
本帖最后由 sanguodouble1 于 2014-4-18 11:23 编辑

兄弟,你这个逻辑这真没法说,
你开了一个Produce11的线程,又开了一个Cont线程,对吧?
先说Producel1线程吧,执行的get_1方法,对吧,问题你进去后就wait了,那就看Cont线程了
但你Cont线程运行的时候flag又是false,也就是进不去set方法,
一个在一直wait,一个在run里面死循环,那你说无语不无语?

哎,这代码,这问题,估计技术分也拿不到,还看得我眼花

评分

参与人数 1黑马币 +1 收起 理由
枫儿 + 1 很给力!

查看全部评分

回复 使用道具 举报
  1. public synchronized void set(String name)
  2. {
  3.                 while(flag)
  4.                         {try{wait();}catch(Exception e){}}
  5.                 this.name = name+"--"+count++;
  6.                 System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
  7.                 flag = true;
  8.                 this.notifyAll();
  9. }
  10. public synchronized void get_1(String name)
  11. {
  12.    while(!flag)
  13.            {try{wait();}catch(Exception e){}}//这里加个“}”
  14.    this.name = name+"--"+count++;
  15.    System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
  16.    flag = false;//这里改成false 否则你的线程1别想执行
  17.    this.notifyAll();
  18. }
  19. }//这里减个“}”
复制代码

朋友,while(flag)判断标记是否使本线程等待,你却吧所有的代码都写到while代码块里去了,flag默认为false,你的线程1,一致性,while不满足相当于没有代码,线程2while满足了,执行第一行代码线程就等待,第一个根本就不能执行,第二个却一直等待,你应该把代码改改!我帮你改了下,不过这样即使运行了,我不知道你的目的是什么,两个线程运行的是相同的代码!还有朋友,你要注意下代码书写,还有不要太粗心了,这个错误太低级了

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
曹冬明 发表于 2014-4-18 12:18
朋友,while(flag)判断标记是否使本线程等待,你却吧所有的代码都写到while代码块里去了,flag默认为false ...

非常感谢
回复 使用道具 举报
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("++消费++");}
}
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马