码迷,mamicode.com
首页 > 编程语言 > 详细

多线程-2

时间:2017-06-08 01:31:00      阅读:132      评论:0      收藏:0      [点我收藏+]

标签: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());

}
}

多线程-2

标签:read   序号   void   构造   rri   通过   情况   get   []   

原文地址:http://www.cnblogs.com/yimimiyi/p/6959766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!