class NewThread implements Runnable{
Thread t;

NewThread(){
t = new Thread(this,"Demo Thread");//想请教下这个this的作用,指向的是哪个对象,请高手帮帮忙,谢谢啦
System.out.println("Child thread: " + t);
t.start();
}
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("child thread: " + i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println("child interrupted.");
}
System.out.println("Exiting child thread.");
}
}class ThreadDemo{
public static void main(String args[]){
new NewThread();

try{
for(int i=5;i>0;i--){
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}catch(InterruptedException e ){
System.out.println("main thread interrupted.");
}
System.out.println("main thread exiting.");
}
}

解决方案 »

  1.   

    指的是NewThread实例对象本身,类的构造方法中的this指的是该类实例化对象本身
    和如下调用结果相同:         NewThread t=new NewThread();
            Thread th=new Thread(t);
            th.start();
            //////////////////
            Thread th=new Thread(new NewThread());
            th.start();        
      

  2.   

    在任何地方使用this都是指向当前对象。作用是为线程指定父线程。
      

  3.   

    this是指代当前这个内部类的对象,  外部类的this要用外部类名.this