package com.Thread;public class NewThread implements Runnable{
Thread t ;
public NewThread(){
t = new Thread(this,"Thread Demo");
System.out.println("Child Thread:" + t);
t.start();
} public void run() {
    try{
       for(int i = 0; i <5; i ++){
 System.out.println("Chile Thread:" + i );
 Thread.sleep(500);
       }
    }catch(Exception e){
      e.printStackTrace();
    }
            System.out.println("Exit Childe Thread");
} }class ThreadDemo{
public static void main(String args[]){
  new NewThread();
  try{
for(int i = 0 ; i < 5; i ++){
System.out.println("Main Thread:" + i );
hread.sleep(1000);
}
  } catch (InterruptedException e) {
e.printStackTrace();
  }
        }

}

解决方案 »

  1.   

    main
    NewThread的ctor/*构造函数*/
    Thread的ctor,传递的是this,this是一个Runnbale的实现类对象.
    start以上是在主线程的,而在start之后,是另外一个线程的运作了:
    run() 跑~嗯,够了吧?
      

  2.   

    哪个是主线程?主线程是实现Runnable接口的类的本身吗?子线程是t = new Thread(this,"Thread Demo");创建的吗?t.start()是启动子线程吗?进到run()以后 是不是等循环结束程序就结束了?问题很愚昧,请楼上的帮忙解答!
    很想补充点JAVA线程的知识!
      

  3.   

    t.start()就是启动线程 
    这个程序  new NewThread();后,跑到其构造方法里,其构造方法是真正建立线程和启动新建线程的地方.当t.start()后会去跑RUN方法.
    RUN方法结束后只能说这个线程结束,并不代表程序结束,一个程序可能有好几个线程 
     
      

  4.   

    我个人觉得首先是
    public NewThread(){
    t = new Thread(this,"Thread Demo");
    System.out.println("Child Thread:" + t);
    t.start();
    }
    这个程序块先执行,因为它是构造函数,而着里面的this指的是main函数。而这个构造函数里面又调用了t.start(),个人觉得下来执行的是run方法,最后执行:
    try{
    for(int i = 0 ; i < 5; i ++){
    System.out.println("Main Thread:" + i );
    Thread.sleep(1000);
    }
      } catch (InterruptedException e) {
    e.printStackTrace();
      }
    ===================================================================================
    我把程序修改了下:class NewThread implements Runnable{
    Thread t ;
    public NewThread(){
    t = new Thread(this,"Thread Demo");
    System.out.println("Child Thread:" + t);
    t.start();
    } public void run() {
        try{
           for(int i = 0; i <5; i ++){
     System.out.println("Chile Thread:" + i );
     Thread.sleep(500);
           }
        }catch(Exception e){
          e.printStackTrace();
        }
                System.out.println("Exit Childe Thread");
    } }public class ThreadDemo 
    {
    public static void main(String args[])throws Exception
         {
          Runnable run= new NewThread();Thread tt=new Thread(run);tt.join();
          try{
           for(int i = 0 ; i < 5; i ++)
       {
               System.out.println("Main Thread:" + i );
               Thread.sleep(1000);
                }
              }
     catch (InterruptedException e) 
     {
       e.printStackTrace();
             }
         }

    }
    想看看join方法对其的影响,个人对join方法不怎么理解,发现每次运行结果不完全相同。还是有些不怎么明白,建议楼主看看thinking in java里面的初始化顺序的篇幅。