标签:read 序号 void 构造 rri 通过 情况 get []
多线程线程名,getName,setName
1.线程名默认情况下会是Thread-0(序号)的形式,线程序号从0开始递增。我们可以通过getName()方法获取线程名称;可以通过setName()方法设置线程名称;Tread也提供了线程名的有参构造。
实例代码:
public class Thread2 extends Thread {
	public Thread2(){
		
	}
	
	//设置线程名称的有参构造
	public Thread2(String name){
		super(name);
	}
	
	//getName() 获取当前线程名称的方法
	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("线程"+getName() + " : " + i);
		}
	}
}
public class TestThread2 {
	public static void main(String[] args) {
//		Thread2  thread = new Thread2();
//		Thread2  thread2 = new Thread2();
//		
//		thread.start();
//		thread2.start();
		
		Thread2 thread = new Thread2();
		Thread2 thread2 = new Thread2("汪汪");
		Thread2 thread3 = new Thread2();
		thread3.setName("飞飞");
		
		thread.start();
		thread2.start();
		thread3.start();
		
		//public static Thread currentThread():返回当前正在执行的线程对象
		System.out.println(Thread.currentThread().getName());
	}
}
标签:read 序号 void 构造 rri 通过 情况 get []
原文地址:http://www.cnblogs.com/yimimiyi/p/6959766.html