黑马程序员技术交流社区
标题:
请帮忙看下以下代码为什么会发生死锁(多线程 唤醒等待机制)
[打印本页]
作者:
唐志海
时间:
2013-11-19 19:01
标题:
请帮忙看下以下代码为什么会发生死锁(多线程 唤醒等待机制)
class resouse
{
private String name;
private String sex;
private boolean flag=false;
public synchronized void set(String name,String sex)
{
while(flag)
try{this.wait();}
catch(Exception e)
{
}
this.name=name;
this.sex=sex;
System.out.println(name+"%……………………………………"+sex);
flag=true;
this.notifyAll();
}
public synchronized void prit()
{
while(!flag)
try{this.wait();}
catch(Exception e){}
System.out.println(name+"....."+sex);
flag=false;
this.notifyAll();
}
}
class Shengchang implements Runnable
{
private resouse r;
Shengchang(resouse r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("李四","男");
}
}
}
class outprint implements Runnable
{
private resouse r;
outprint(resouse r)
{
this.r=r;
}
public void run()
{
r.prit();
}
}
public class Dengdai {
public static void main(String[] args)
{
resouse r=new resouse();
Shengchang in=new Shengchang(r);
outprint ou=new outprint(r);
Thread t=new Thread(in);
Thread t1=new Thread(in);
Thread t2=new Thread(ou);
Thread t3=new Thread(ou);
t1.start();
t.start();
t2.start();
t3.start();
}
}
复制代码
作者:
hurryup
时间:
2013-11-19 19:57
资源方法里面少大括弧。
作者:
唐志海
时间:
2013-11-19 20:48
没有吧,少个大括弧那编译都不会通过啊。
作者:
黄炳期
时间:
2013-11-20 09:33
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2