public class SrThread2         
{
public static void main(String args[])
{
SubThread another=new SubThread();  
another.start();     
while(another.isAlive())  //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立
                          //非常奇怪.
{
try{Thread.sleep(100);}   //这个Thread为什么一定指的是主线程,
                           //它只是一个类名
catch(InterruptedException e){return;}
System.out.println("mainthread is running");
}
}
}
class SubThread extends Thread 
{
public void run()
{
for (int i=0;i<3;i++)
{
System.out.println(this.getName()+" is running");
}
}
}

解决方案 »

  1.   

    1.你怎么知道another线程执行完了?
    while(another.isAlive())  //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立
                                      //非常奇怪.
            {
              //在这加一句输出,看看最后的输出情况
               System.out.println(this.getName()+" is running"); 
                try{Thread.sleep(100);}   //这个Thread为什么一定指的是主线程,
                                           //它只是一个类名
                catch(InterruptedException e){return;}
                System.out.println("mainthread is running");
            }
    2.去看sleep方法的文档,
    sleep
    public static void sleep(long millis,
                             int nanos)
                      throws InterruptedException在指定的毫秒数加指定的纳秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。
    让当前线程休眠,你放在main线程里当然休眠的是main了,你把
    try{Thread.sleep(100);}   //这个Thread为什么一定指的是主线程,
                                           //它只是一个类名
                catch(InterruptedException e){return;}
    放在另一个线程的main方法里休眠的就是另一个线程了.结贴吧.
      

  2.   

    try{Thread.sleep(100);}   //这个Thread为什么一定指的是主线程,
    在哪个线程中调用Thread.sleep()函数就是指的哪个线程,你这里是Main函数调用的,Thread当然就是指的主线程了。如果你是在类SubThread的run()函数中加入了Thread.sleep()函数调用这个Thread就是指导的SubThread进程了。其实这个说法还有些问题,sleep()方法是Thread类的静态方法,public static void sleep(long millis) throws InterruptedException在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。

     another.start();                 
    while(another.isAlive())  //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立
     another.start();  只是启动了一个SubThread线程而已,它并不会一直等到SubThread线程执行完毕之后才往下执行下一条while(another.isAlive())指令,在Java的多线程机制中,主线程和子线程是交替执行的。所以整个运行过程中main线程和SubThread子线程是交替执行的,在SubThread没有执行完毕的时候main线程也可以接着往下执行。
    这是本人的一点见解,不知对楼主可有帮助。如果不当之处也请各位兄台批评指正。
      

  3.   

    学习!main方法是个静态的方法
             //在这加一句输出,看看最后的输出情况 
              System.out.println(this.getName()+" is running"); 
    加不了this啊!
      

  4.   

    忘了给lz改了.可以使用 Thread.currentThread().getName()
    我太笨了,不好意思.
      

  5.   

    你一直想要的严谨的交替执行代码
    你上个帖子结了,发到这吧,本贴名为再论,原论还没结贴呢。public class Maintest {
    public static void main(String args[]) throws Exception {
    ThreadDemo thr = new ThreadDemo();
    thr.start();
    for (int i = 1; i < 1000; i++) {
    synchronized (Maintest.class) {
    System.out.println("main: " + i);
    Maintest.class.notify();
    Maintest.class.wait();
    }
    } }
    }class ThreadDemo extends Thread {
    public void run() {
    for (int i = 1; i < 1000; i++) {
    try {
    synchronized (Maintest.class) {
    System.out.println(this.getName() + ":" + i);
    Maintest.class.notify();
    Maintest.class.wait();//会释放锁
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
      

  6.   

    try{Thread.sleep(100);}   //这个Thread为什么一定指的是主线程,
                                           //它只是一个类名
    多线程的main线程里这样已经说明。
    它已经进入这个条件判断句了。
    所以不是dead状态的时候才跳入的。
    main的优先级为5
    gc()的优先级最低,所以收回内存并不是程序员能马上控制的
      

  7.   

    按照LZ的意思的话,得把SubThread 定为后台
    线程一旦创建,那么享受的待遇都是一样的(没优先级和后台情况下),
    谁规定一只猪死了,由此猪生下的小猪就得跟着死了
      

  8.   

    你的误解是another.isAlive()引起, 楼主要去了解这个方法一下。
    java线程中没有  正在运行的线和程可运行的线程之分别,它们统一是alive。
    public class SrThread2                         
    {
        public static void main(String args[])
        {
            SubThread another=new SubThread();  
            another.start();                 
            while(another.isAlive())  //another线程还没有运行,因为another是可运行线程,所以another.isAlive()返回true.
            {
                try{Thread.sleep(100);}   //这个Thread不一定指的是主线程,它指的是当前线程。
                   //当前线程sleep后,计算机调雅another线程运行。
                catch(InterruptedException e){return;}
                System.out.println("mainthread is running");
            }
        }
    }
    class SubThread extends Thread             
    {
        public void run()
        {
            for (int i=0;i<3;i++)
            {
                System.out.println(this.getName()+" is running");    
            }
        }
    }
      

  9.   

    你们都没看明白,其实是楼主现在还保持着过程编程的思想,他以为执行了another.start();这句,main线程就停下不动,等着子线程运行完了才继续走while语句,所以才会有那个疑问。