标签:提高 时间段 alt 路径 nal call java虚拟机 重写 好处
package java012; /** * 2017/9/29 * 说明: */ class Demo { public void finalize(){ System.out.println("我要死了"); } } public class ThreadDemo { public static void main(String[] args) { new Demo(); new Demo(); System.gc(); new Demo(); System.out.println("你好世界"); } }
package java012; /** * 2017/9/29 * 说明: */ class Demo extends Thread { private String name; public Demo(String name){ this.name = name; } public void run(){ this.show(); } public void show(){ for(int x =0;x<100;x++){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name+":"+x); } } } public class ThreadDemo { public static void main(String[] args) { Demo d1 = new Demo("d1"); Demo d2 = new Demo("d2"); d1.start(); d2.start(); } }
public final String getName()
package java012; /** * 2017/9/29 * 说明: */ class Demo extends Thread { private String name; public Demo(String name){ this.name = name; } public void run(){ this.show(); } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+getName()); } } } public class ThreadDemo { public static void main(String[] args) { Demo d1 = new Demo("d1"); Demo d2 = new Demo("d2"); d1.run(); d2.run(); } }
结果好像不对,因为我们并没有调用start()方法,那么此时运行的打印的线程的名称应该是main之类的。对,应该打印的是main之类的,但是,我们要知道,我们调用的是Thread的无参构造方法。代码如下所示:
package java012; /** * 2017/9/29 * 说明: */ class Demo extends Thread { private String name; public Demo(String name){ super();//调用父类的无参构造方法 this.name = name; } public void run(){ this.show(); } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+getName()); } } } public class ThreadDemo { public static void main(String[] args) { Demo d1 = new Demo("d1"); Demo d2 = new Demo("d2"); d1.run(); d2.run(); } }
那么,此时我们观察Thread的源码就可以知道答案:
/** * Allocates a new {@code Thread} object. This constructor has the same * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} * {@code (null, null, gname)}, where {@code gname} is a newly generated * name. Automatically generated names are of the form * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. */ public Thread() { init(null, null, "Thread-" + nextThreadNum(), 0); }
这下知道原因了吧。
public static Thread currentThread()
package java012; /** * 2017/9/29 * 说明: */ class Demo extends Thread { private String name; public Demo(String name){ super();//调用父类的无参构造方法 this.name = name; } public void run(){ this.show(); } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+Thread.currentThread().getName()); } } } public class ThreadDemo { public static void main(String[] args) { Demo d1 = new Demo("d1"); Demo d2 = new Demo("d2"); d1.run(); d2.run(); } }
上面的代码证明了2点:①如果我们不调用自定义线程的start()方法,就不是开启新的线程②想获取运行时的线程,必须通过Thread.currentThread()。
package java012; /** * 2017/9/29 * 说明: */ class Demo extends Thread { private String name; public Demo(String name){ super();//调用父类的无参构造方法 this.name = name; } public void run(){ this.show(); } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+Thread.currentThread().getName()); } } } public class ThreadDemo { public static void main(String[] args) { Demo d1 = new Demo("d1"); Demo d2 = new Demo("d2"); d1.start(); d2.start(); System.out.println("主线程的名字:"+Thread.currentThread().getName()); } }
public Thread(String name)
package java012; /** * 2017/9/29 * 说明: */ class Demo extends Thread { private String name; public Demo(String name){ super(name);//调用父类的有参构造方法,给线程取定义的名称 this.name = name; } public void run(){ this.show(); } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+Thread.currentThread().getName()); } } } public class ThreadDemo { public static void main(String[] args) { Demo d1 = new Demo("d1"); Demo d2 = new Demo("d2"); d1.start(); d2.start(); System.out.println("主线程的名字:"+Thread.currentThread().getName()); } }
标签:提高 时间段 alt 路径 nal call java虚拟机 重写 好处
原文地址:http://www.cnblogs.com/xuweiweiwoaini/p/7612585.html