就我对线程的生命周期的理解是:
 1.新创建一个Thread类(或继承Thread类)的实例,在内存中拥有一块存储空间.
 2.调用XX.start()方法 (由它自己调用run()方法)初始化线程并启动此线程,这时线程已经开始。
 3.当run()方法运行完 (应该是线程结束的时候吗?)我想问的是run()顺利运行完,就真的是结束吗?那为什么再次start()时会报: IllegalThreadStateException - 如果线程已经启动。(Thread类中有个属性start 仍然为ture,证明这个类的实例的状态仍然为启动状态)在Thread类中: isInterrupted() 判断线程是否中断  
run()运行完后 isInterrupted() 状态仍然为false,即:没有被中断
如果用interrupt();也仍然不能再次start()那怎么能是这个线程停止?
如果让继承Thread类的类重新生成一个实例,那当然没话说,但这不是答案,其中:stop(),destory()这种已过时,且存在问题的方法不与考虑。

解决方案 »

  1.   

    run()方法运行完应该是线程结束,至于到底是什么原因把代码贴出来看看喽
      

  2.   

    你自己可以做个实验看看嘛,你调试时就会发现并不是那么简单的理解public class Test extends Thread{
         private int i=0;
            public void run(){
                 i = i+100;
            }   public static void main(String[] args){
              Test t = new Test();
              t.start();
              Thread.sleep(1000);//间断1秒
                t.start();
       }
    }
    以上是我随手写的,楼下的可以自己添加 interrupt();等方法调试下就知道了
      

  3.   

    现在贴出来的是java1.5 提供的 Thread类
    public
    class Thread implements Runnable {
        /* Make sure registerNatives is the first thing <clinit> does. */
        private static native void registerNatives();
        static {
            registerNatives();
        }    private char name[];
        private int         priority;
        private Thread threadQ;
        private long eetop;
        private boolean     started; // true iff this thread has been started..........
        public synchronized void start() {
            if (started)
                throw new IllegalThreadStateException();
            started = true;
            group.add(this);
            start0();
        }
        .....
    }在这里可以很清楚的看到start() 这个方法一开始就判断属性 started的值
    当线程启动,运行完后这个值似乎依旧是started=true;
    运行完后这个值就没改过
      

  4.   

    你在第二次start之前加  System.out.println(t.isAlive());
    结果是false,是否可以认为t已经死了,所以不能再start?
      

  5.   

    这也是个疑惑的地方,按JAVA文档说明是:测试线程是否处于活动状态。如果线程已经启动且尚未终止,则为活动状态。
    这句话有两个条件:
      1.线程已经启动
      2.且没有终止
    这时才返回true;那是不是说启动就一定未终止呢?
    换句话说,是不是有可能虽然线程已启动但却是停止状态(而不是“尚未终止”状态)呢?这个方法表示线程是否是活动的,难道不活动就是完结了?
      

  6.   


    public class ThreadTest extends Thread{
    public static void main(String[] args){ 
    ThreadTest t = new ThreadTest(); 
    System.out.println(t.isAlive());
            t.start();
            System.out.println(t.isAlive());
            try{
            Thread.sleep(2000);//间断1秒
            }catch(InterruptedException e){}
            System.out.println(t.isAlive());

     public void run(){ 
             int i = 1;
             System.out.println(Thread.currentThread().isAlive());
             System.out.println("第" +i +"次离开");
         } 
    }这样是否好理解点,输出结果是:
    false
    true
    true
    第1次离开
      

  7.   

    false
    true
    true
    第1次离开
    false
    这才是楼上的输出结果
      

  8.   


    public class ThreadTest extends Thread{
        private int i = 0;    public static void main(String[] args){ 
         ThreadTest t = new ThreadTest();     System.out.println("1:"+t.isAlive());//1
            t.start();        System.out.println("2:"+t.isAlive());//2
            try{
            Thread.sleep(2000);//间断1秒
            }catch(InterruptedException e){}
            System.out.println("3:"+t.isAlive());//3
            
        }
        
         public void run(){
             i++;
             System.out.println("run:"+Thread.currentThread().isAlive());
             System.out.println("第" +i +"次离开");
         }
    }
    在这段代码能说明isAlive()什么呢?
    宏观来看确实是当run()运行完后线程状态 isAlive()=false;(这点似乎让 t 这个 ThreadTest的 实例回到了起点)
    但成false之后能再start();吗?仍然报错:
           1:false
          2:true
          run:true
          第1次离开
           3:false      Exception in thread "main" java.lang.IllegalThreadStateException
       at java.lang.Thread.start(Thread.java:571)
       at ThreadTest.main(ThreadTest.java:18)
    结果是我仍然没办法再次使用这个实例
      

  9.   

    线程启动以后,是不能再次启动的,你可以搜一下JDK的源代码,只有started=true,却没有使started=false的方法,因为线程是一次性的,不同于其他的类.
      

  10.   

    不知道线程的生命周期是不是该如下理解:
      ThreadTest t = new ThreadTest(); // t 为实例对象
      
      t.start();//线程开始启动 isAlive()=true;
      //当run(); 运行完毕 isAlive()=false;  表示线程结束
    线程的生命周期结束  但 t 的实例对象生命周期仍然未结束,从对象的思想考虑它就不该线程来管了????
    对不?