class shen implements Runnable
{
xuni xu;
public shen(xuni xu)
{
this.xu=xu;
}
public void run()
{
  int x=0;
while(true)
{
  synchronized(xu)
  {
   try
   {
   if(xu.bfull)
   {
   xu.wait();
   }
   }
   catch(Exception ex)
   {
  
   }
if(x==0)
{
xu.name="lizhao";
//try{Thread.sleep(1);}catch(Exception ex){}
xu.sex="man";
}
else
{
xu.name="xingyu";
xu.sex="male";
}
xu.bfull=true;
xu.notify();
}
x=(x+1)%2;
}
}
}
class xiao implements Runnable
{
xuni xu;
public xiao(xuni xu)
{
this.xu=xu;
}
public void run()
{
while(true)
{
    synchronized(xu)
    {
     try
     {
     if(!xu.bfull)
     {
     xu.wait();
     }
     }
catch(Exception ex)
{

}
System.out.print(xu.name+":");
  System.out.println(xu.sex);
  xu.bfull=false;
  xu.notify();
  }
}
}
}
class xuni
{
String name="no";
String sex="no";
boolean bfull=false;
}
class zhu
{
public static void main(String[] args)
{
xuni xu2=new xuni();
new Thread(new shen(xu2)).start();
new Thread(new xiao(xu2)).start();
}
}以上的程序是先运行的哪个线程?
shen类中run()方法中
if(xu.bfull)括号中的xu.bfull值是true还是false呀?xiao类中run()方法中
if(!xu.bfull)括号中的xu.bfull值是true还是false呀?程序的运行顺序是怎么样的呀?谁能给解读一下.

解决方案 »

  1.   

    自己debug一下不就什么都清楚了?
      

  2.   

    我把你程序改了下,不知道你能能看明白程序的流程了。
    package test;class Test {
    public static void main(String[] args) {
    xuni xu2 = new xuni();
    new Thread(new shen(xu2)).start();
    new Thread(new xiao(xu2)).start();
    }
    }class shen implements Runnable {
    xuni xu; public shen(xuni xu) {
    System.out.println("调用了shen中的shen()");
    this.xu = xu;
    } public void run() {
    System.out.println("调用了shen中的run()");
    int x=0;

    while(x<5){
    x++;
    synchronized (xu) {
    try {
    if (!xu.bfull) {//当为flase 进入循环体
    xu.notify();//唤醒进程
    //System.out.println("=====================elseelseelseelse");
    xu.name = "xingyu1";
    xu.sex = "male1";
    System.out.print(xu.name + ":");
    System.out.println(xu.sex);
    xu.bfull = true;

    }
    else{//当为true 进入循环体
    xu.wait();// 线程等待
    }


    } catch (Exception ex) { }

    }
    }
    }
    }class xiao implements Runnable {
    xuni xu; public xiao(xuni xu) {
    System.out.println("调用了xiao中的xiao()");
    this.xu = xu;
    } public void run() {
    System.out.println("调用了xiao中的run()");
    int x=0;
    while (x<5) {
    x++;
    synchronized (xu) {
    try {
    if (!xu.bfull) {
    //System.out.println("=========="+xu.bfull);
    xu.wait();//唤醒进程
    }
    else{
    xu.notify();// 线程等待
    xu.name = "xingyu2";
    xu.sex = "male2";
    System.out.print(xu.name + ":");
    System.out.println(xu.sex);
    xu.bfull = false;

    }

    } catch (Exception ex) { }

    }
    }
    }
    }
    class xuni{
    String name = "no"; String sex = "no"; boolean bfull = false;
    }