参考:http://bbs.csdn.net/topics/391872079
1 package thread.demo01; 2 3 public class MyThread extends Thread { 4 5 public MyThread() { 6 System.out.println("构造方法的打印 : " + Thread.currentThread().getName()); 7 } 8 9 @Override 10 public void run() { 11 System.out.println("run方法的打印: " + Thread.currentThread().getName()); 12 } 13 }
1 package thread.demo01; 2 3 public class Run2 { 4 5 public static void main(String[] args) { 6 MyThread myThread = new MyThread(); 7 myThread.start(); 8 } 9 10 }
MyThread的构造方法是main现成调用的
而run方法是被新开的线程调用
所以输出结果是:
1 package thread.demo01; 2 3 public class Run2 { 4 5 public static void main(String[] args) { 6 MyThread myThread = new MyThread(); 7 // myThread.start(); 8 myThread.run(); 9 } 10 11 }
若更改代码为如上,run方法则是main线程调用,所以输出结果:
----------------------------------------------------------------------------------
1 package thread.demo01; 2 3 public class CurrentOperate extends Thread { 4 5 public CurrentOperate() { 6 System.out.println("CountOperate -- begin"); 7 System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()); 8 System.out.println("this.getName() = " + this.getName()); 9 System.out.println("CountOperate -- end"); 10 } 11 12 @Override 13 public void run() { 14 System.out.println("run -- begin"); 15 System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()); 16 System.out.println("this.getName() = " + this.getName()); 17 System.out.println("run -- end"); 18 } 19 20 }
1 package thread.demo01; 2 3 public class Run { 4 5 public static void main(String[] args) { 6 CurrentOperate c = new CurrentOperate(); 7 Thread c1 = new Thread(c); 8 c1.start(); 9 } 10 11 }
和上面一样,这边的有个 Thread-1,是因为 CurrentOperate 传给了 Thread,所以
调用run方法 不是main而是c1所在的线程。
Thread.currentThread().getName()是获得调用这个方法的线程的名字,在main线程中调用的当然是main了。
而this.getName()这个方法是获取当前对象的名字,只是单纯的方法的调用。
----------------------------------------------------------------------------------------------
1 package thread.demo02; 2 3 public class CountOperate extends Thread { 4 5 public CountOperate() { 6 System.out.println("CountOpreate -- begin"); 7 System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()); 8 System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive()); 9 System.out.println("this.getName() = " + this.getName()); 10 System.out.println("this.isAlive() = " + this.isAlive()); 11 System.out.println("CountOpreate -- end"); 12 } 13 14 @Override 15 public void run() { 16 System.out.println("run -- begin"); 17 System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()); 18 System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive()); 19 System.out.println("this.getName() = " + this.getName()); 20 System.out.println("this.isAlive() = " + this.isAlive()); 21 System.out.println("run -- end"); 22 } 23 24 }
1 package thread.demo02; 2 3 public class Run { 4 5 public static void main(String[] args) { 6 CountOperate c = new CountOperate(); 7 Thread t1 = new Thread(c); 8 System.out.println("main begin t1 isAlive = " + t1.isAlive()); 9 // t1.setName("A"); 10 t1.start(); 11 System.out.println("main end t1 isAlive = " + t1.isAlive()); 12 } 13 14 }