标签:code dem throw class import hit except 存在 throws
使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但是使用this调用getName()方法只能在本类中,而不能在其他类中,更不能在Runnable接口中,所以只能使用Thread.currentThread().getName()获取线程的名称,否则会出现编译时异常。
class DemoThread extends Thread{ public DemoThread(){ } @Override public void run() { super.run(); System.out.println("内部 this.isAlive" + this.isAlive()); System.out.println("内部 Thread.currentThread().isAlive()"+Thread.currentThread().isAlive()); System.out.println("内部 this.getName" + this.getName()); System.out.println("内部 Thread.currentThread().getName()"+Thread.currentThread().getName()); } } public static void main(String[] args) throws InterruptedException { DemoThread d = new DemoThread(); Thread t1 = new Thread(d); t1.setName("213"); t1.start(); System.out.println("外部t1.isAlive()"+t1.isAlive()); System.out.println("外部t1.getName()"+t1.getName()); }Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
标签:code dem throw class import hit except 存在 throws
原文地址:https://www.cnblogs.com/zjm-1/p/9513334.html