线程的等待和唤醒代码运行不了求助

XyGeL 2016-12-11 10:59:56


public class producter implements Runnable {
private Info info;

public producter(Info info) {
super();
this.info = info;
}
@Override
public void run() {
boolean flag = false;
for (int i = 0; i < 50; i++) {
if (flag) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setNameCntent("小A", "JAVA学员");

flag = false;
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setNameCntent("mol", "www.baidu.com");
flag = true;
}

}

}
}

上面是生产类



public class Consumer implements Runnable {
private Info info;

public Consumer(Info info) {
super();
this.info = info;
}

@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.getNameContent();

}
}
}


上面是用户类


public class Info {
private String name = "小A";// 保存信息用
private String content = "JAVA学员";// 保存信息用
private boolean flag = false;

public synchronized void setNameCntent(String name, String Content) {
if (!flag) {
try {
super.wait();// 等待消耗者消耗
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;

this.content = Content;
flag = false;// 生产完了修改标识
super.notify();
}
}

public synchronized void getNameContent() {
if (flag) {
try {
super.wait();// 等待生产者生产

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.name + "---" + this.content);
flag = true;
super.notify();
}
}
}

上面是基本类


public class Demo {
public static void main(String[] args) {
Info i = new Info();
producter p = new producter(i);
Consumer c = new Consumer(i);
Thread t = new Thread(p);
Thread t2 = new Thread(c);

t.start();
t2.start();

}
}

最后是测试类

不管怎么运行就是运行不起走,求大神告知怎么回事啊,上面代码可以直接复制到eclipse上测试,思考改码都快2小时了,简直头大
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
soton_dolphin 2016-12-12
  • 打赏
  • 举报
回复
info 类

public class Info {
    private String name = "小A";// 保存信息用
    private String content = "JAVA学员";// 保存信息用
    private boolean flag = false;
 
    public synchronized void setNameCntent(String name, String Content) {
        while (flag) {
            try {
                wait();//  等待消耗者消耗
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        this.name = name;
        
        this.content = Content;
    	flag = true;
        notifyAll();
    }
 
    public synchronized void getNameContent() {
        while(!flag) {
            try {
                wait();// 等待生产者生产
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        

        System.out.println(this.name + "---" + this.content);
        flag = false;// 生产完了修改标识
        notifyAll();
    }
}
Producer

public class Producer implements Runnable {
    private Info info;
 
    public Producer(Info info) {
        this.info = info;
    }
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            info.setNameCntent("小A", "JAVA学员");
        }
    }
}
Consumer

public class Consumer implements Runnable {
    private Info info;
 
    public Consumer(Info info) {
        super();
        this.info = info;
    }
 
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            info.getNameContent();
        }
    }
}
爱睡觉的阿狸 2016-12-12
  • 打赏
  • 举报
回复
把Info类这样改下就没问题了:
public class Info {
    private String name = "小A";// 保存信息用
    private String content = "JAVA学员";// 保存信息用
    private boolean flag = false;
 
    public synchronized void setNameCntent(String name, String Content) {
        if (!flag) {
        	flag = true;// 生产完了修改标识
            try {
                super.wait();//  等待消耗者消耗
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.name = name;
            this.content = Content;
            super.notify();
        }
    }
 
    public synchronized void getNameContent() {
        if (flag) {
        	flag = false;
            super.notifyAll();;// 等待生产者生产
            try {
                Thread.sleep(100);
                System.out.println(this.name + "---" + this.content);
                super.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
XyGeL 2016-12-12
  • 打赏
  • 举报
回复
是啊,我今天才发现,如果先执行这段,就直接卡主了,所以该怎么去修改呢
游一游走一走 2016-12-11
  • 打赏
  • 举报
回复
如果我先执行这个代码,请问如何唤醒

    public synchronized void setNameCntent(String name, String Content) {
        if (!flag) {
            try {
                super.wait();//  等待消耗者消耗
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.name = name;
             
            this.content = Content;
            flag = false;// 生产完了修改标识
            super.notify();
        }
    }

62,628

社区成员

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

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