黑马程序员技术交流社区
标题:
等待唤醒机制为什么没输出?
[打印本页]
作者:
刘松老师
时间:
2013-3-12 21:33
标题:
等待唤醒机制为什么没输出?
写的等待唤醒机制为什么什么也没输出?不解....
package com.thread;
public class DoubleRunnable {
public static void main(String[] args) {
Person p = new Person();
new Thread(new Input(p)).start();
new Thread(new OutPut(p)).start();
}
}
class Input implements Runnable
{
private Person p;
Input(Person p){
this.p=p;
}
public void run(){
boolean b = true;
while(true){
if(b=true){
p.set("zhangsan", "nan");
b=false;
}else{
p.set("小小", "女");
b=true;
}
}
}
}
class OutPut implements Runnable
{
private Person p;
OutPut(Person p){
this.p=p;
}
public void run(){
while(true){
p.out();
}
}
}
class Person{
private String name;
private String sex;
private boolean flag=false;
public synchronized void set(String name,String sex){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.name=name;
this.sex=sex;
flag=true;
this.notify();
}
}
public synchronized void out(){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"=="+sex);
flag=false;
this.notify();
}
}
}
作者:
王智威
时间:
2013-3-12 22:01
本帖最后由 王智威 于 2013-3-12 22:06 编辑
你在睡下去子前要叫交醒别人,而不是睡了再交醒别人。
下面是我该过的代码:
public class DoubleRunnable {
public static void main(String[] args) {
Person p = new Person();
new Thread(new Input(p)).start();
new Thread(){
};
new Thread(new OutPut(p)).start();
}
}
class Input implements Runnable
{
private Person p;
Input(Person p){
this.p=p;
}
public void run(){
boolean b = true;
while(true){
if(b){
p.set("zhangsan", "nan");
b=false;
}else{
p.set("小小", "女");
b=true;
}
}
}
}
class OutPut implements Runnable
{
private Person p;
OutPut(Person p){
this.p=p;
}
public void run(){
while(true){
p.out();
}
}
}
class Person{
private String name;
private String sex;
private boolean flag=true;
public synchronized void set(String name,String sex){
if(flag){
this.name=name;
this.sex=sex;
flag=false;
this.notify();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void out(){
if(!flag){
System.out.println(name+"=="+sex);
flag=true;
this.notify();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
下面是运行结果:
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
小小==女
zhangsan==nan
希望是你想要的
作者:
孙晋学
时间:
2013-3-12 22:50
两处错误:第一
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.name=name;
this.sex=sex;
flag=true;
this.notify();
} //你这个if的括号有点远,它是用来判断该线程是否等待,把后面都括起来,改变标志位和唤醒线程都执行不到了
}
复制代码
第二个:是个低级错误
if(b=true){ //判断不是赋值 if(b==true)
p.set("zhangsan", "nan");
b=false;
}else{
p.set("小小", "女");
b=true;
}
复制代码
作者:
刘松老师
时间:
2013-3-13 09:55
孙晋学 发表于 2013-3-12 22:50
两处错误:第一第二个:是个低级错误
哦,原来如此,谢谢啦
作者:
猫腻
时间:
2013-3-13 09:56
如果仍有问题,可以继续追问;若没有问题了,请及时将帖子分类改成【已解决】~
http://bbs.itheima.com/thread-37643-1-1.html
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2