class Jub
{
String name;
String age;
boolean nn;
}
class Abs implements Runnable
{
Jub s;
Abs(Jub s)
{
this.s = s;
}
public void run()
{
int x = 0;
while(true)
{
synchronized(s)
{ if(s.nn)try{s.wait();}catch(Exception e){}//等待机制
{
if (x==0)
{
s.name = "nsss";
s.age = "bhddhhd";
}else
{
s.name = "丽丽";
s.age = "女女女";
}
x = (x+1)%2;
s.nn=true;
s.notify();
}
}
}
}
}
class Diao implements Runnable
{
Jub s;
Diao(Jub s)
{
this.s = s ;
}
public void run()
{
while(true)
{ synchronized(s)
{ if (!s.nn)try{s.wait();}catch(Exception e){}
{
System.out.println(s.name+"......"+s.age);
s.nn = false;
s.notify();
}
}
}
}
}
class Day0802
{
public static void main(String [] args)
{ Jub s = new Jub();
Abs a = new Abs(s);
Diao b = new Diao(s);
Thread t1 = new Thread(a);
Thread t2= new Thread(b);
t1.start();
t2.start();
}
} |
|