public class Example8_3{
    public static void main(String args[ ]){
        Left left=new Left();
        Right right=new Right();
        left.start();
        right.start();
        while(true){
          if(left.n==8||right.n==8)
             System.exit(0);
        }  
    }
}
class Left extends Thread{
    int n=0;
    public void run(){
        while(true){
            n++;
            System.out.printf("\n%s","我在左面写字");
            try {  sleep((int)(Math.random()*100)+100);
            }
            catch(InterruptedException e) {}
        }
    }
}
class Right extends Thread{
    int n=0;
    public void run(){
        while(true){
            n++;
            System.out.printf("\n%40s","我在右面写字");
            try {  sleep((int)(Math.random()*100)+100);
            }
            catch(InterruptedException e) {}
        }
    }
}
谢谢大家帮我看看好么?
里面的 while(true){
            n++;
            System.out.printf("\n%40s","我在右面写字");
            try {  sleep((int)(Math.random()*100)+100);
            }
            catch(InterruptedException e) {}
        }
while里面的true是怎么判断的,也就是说,什么情况下,会变成while(false)然后结束循环呢???

解决方案 »

  1.   

    不会结束的.  while(true){
      if(left.n==8||right.n==8)
      System.exit(0); //这里退出.
     
      

  2.   

    谢谢,那下面这个程序呢?
    public class Example8_6{
        public static void main(String args[ ]){
            String s1="张三",s2="Jam.keven"; 
            Move move=new Move(s1,s2);
            Thread zhang,keven;
            zhang=new Thread(move); 
            keven=new Thread(move);
            zhang.setName(s1);
            keven.setName(s2);
            zhang.start();
            keven.start(); 
        }
    }
    class Move implements Runnable{
        String s1,s2;
        Move(String s1,String s2){
           this.s1=s1;
           this.s2=s2; 
        } 
        public void run(){
            int i=0;
            while(true){
               if(Thread.currentThread().getName().equals(s1)){
                   i=i+1;
                   System.out.println(s1+"线程的局部变量:"+i);
                   if(i>=4){
                      System.out.println(s1+"线程进入死亡状态");
                      return;
                   }
               } 
               else if(Thread.currentThread().getName().equals(s2)){
                   i=i-1;
                   System.out.println(s2+"线程的局部变量:"+i);
                   if(i<=-4){
                      System.out.println(s2+"线程进入死亡状态");
                      return;
                   }
                } 
                try{  Thread.sleep(800);
                }
                catch(InterruptedException e) {}
            }
        }
    }
    里面如果采用无参数的构造方法,,就是变成 
    Move(){
            } 
    把上面的对象创建改成 Move move=new Move();
    这样运行没显示错误,但是运行不出结果?请问是什么原因呢?