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.");
}
}
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.");
}
}
和如下调用结果相同: NewThread t=new NewThread();
Thread th=new Thread(t);
th.start();
//////////////////
Thread th=new Thread(new NewThread());
th.start();