從新來看多線程時,被這結果搞懵逼了。很少說,直接上代碼:緩存
1 public class MyThread02 extends Thread { 2 public MyThread02() { 3 System.out.println("init curr: " + Thread.currentThread().getName()); 4 System.out.println("init this: "+this.getName()); 5 } 6 @Override 7 public void run() { 8 System.out.println("run curr: " + Thread.currentThread().getName()); 9 System.out.println("run this: "+this.getName()); 10 } 11 }
1 public class Test02 { 2 public static void main(String[] args) { 3 MyThread02 target = new MyThread02(); 4 Thread thread = new Thread(target); 5 thread.setName("A"); 6 thread.start(); 7 } 8 }
1 init curr: main 2 init this: Thread-0 3 run curr: A 4 run this: Thread-0
1 public Thread(Runnable target) { 2 init(null, target, "Thread-" + nextThreadNum(), 0); 3 }
1 /* For autonumbering anonymous threads. */ 2 private static int threadInitNumber; 3 private static synchronized int nextThreadNum() { 4 return threadInitNumber++; 5 }
1 public void run() { 2 if (target != null) { 3 target.run(); 4 } 5 }