一,常用方法
二,案例
父类:
package ThreadLianXi;import ThreadLianXi.ZhiLeiA;public class Name {public static void main(String[] args)throws Exception{Thread t1 =new ZhiLeiA("1号");//修改名字t1.setName("1号");t1.start();//join方法;让当前方法先执行完t1.join();//获取线程名字System.out.println(t1.getName());//原名:Thread-0//1号Thread t2 =new ZhiLeiA("2号");t2.start();System.out.println(t2.getName());//原名:Thread-1//2号//获取线程对象Thread m =Thread.currentThread();System.out.println(m.getName());//main//暂停5秒(记得在开头写上:throws Exception)for (int i = 0; i <4 ; i++) {System.out.println(m.getName()+"线程输出"+i);if (i==2){Thread.sleep(5000);}}}
}
子类:
package ThreadLianXi;public class ZhiLeiA extends Thread {public ZhiLeiA(String name){super(name);//为线程设置名字}@Overridepublic void run(){Thread m =Thread.currentThread();for (int i = 0; i <4 ; i++) {System.out.println(m.getName()+"线程输出"+i);}}
}