只有线程在创建时命名,才可以用Thread.getName();方法获得名称。。。。
2018-01-16 09:37
549 查看
在线程创建时,不用预留String变量存储线程名称。。。。
在创建时命名:MyThread(String name) {
thrd = new Thread(this, name);
thrd.start();
}
。。。。不是在创建时,命名的运行结果如下。。
![](https://img-blog.csdn.net/20180116093807527?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzk2MzI5MTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Thread-0是线程的名称,下面是源代码:
package bao;
class MyThread implements Runnable {
String thrdName;
MyThread(String name) {
thrdName = name;
}
public void run() {
System.out.println(thrdName+" startinggg.");
try {
for(int count=0; count<10; count++ ) {
Thread.sleep(400);
System.out.println("In "+thrdName+", count is "+count);
}
}
catch(InterruptedException exc) {
System.out.println(thrdName+" interrupted.");
}
System.out.println(thrdName+" interrupting.");
}
}
class D {
public static void main(String[] args) {
System.out.println("Main thread starting.");
MyThread mt = new MyThread("Child #1");
Thread newThrd = new Thread(mt);
System.out.println(newThrd.getName());
newThrd.start();
System.out.println(newThrd.getName());
for(int i=0; i<50; i++ ) {
System.out.print(".");
try {
Thread.sleep(100);
}
catch(InterruptedException exc) {
System.out.println("Main thread interrupted.");
}
}
System.out.println(newThrd.getName());
System.out.println("Main thread ending.");
}
}
在创建时命名:MyThread(String name) {
thrd = new Thread(this, name);
thrd.start();
}
。。。。不是在创建时,命名的运行结果如下。。
Thread-0是线程的名称,下面是源代码:
package bao;
class MyThread implements Runnable {
String thrdName;
MyThread(String name) {
thrdName = name;
}
public void run() {
System.out.println(thrdName+" startinggg.");
try {
for(int count=0; count<10; count++ ) {
Thread.sleep(400);
System.out.println("In "+thrdName+", count is "+count);
}
}
catch(InterruptedException exc) {
System.out.println(thrdName+" interrupted.");
}
System.out.println(thrdName+" interrupting.");
}
}
class D {
public static void main(String[] args) {
System.out.println("Main thread starting.");
MyThread mt = new MyThread("Child #1");
Thread newThrd = new Thread(mt);
System.out.println(newThrd.getName());
newThrd.start();
System.out.println(newThrd.getName());
for(int i=0; i<50; i++ ) {
System.out.print(".");
try {
Thread.sleep(100);
}
catch(InterruptedException exc) {
System.out.println("Main thread interrupted.");
}
}
System.out.println(newThrd.getName());
System.out.println("Main thread ending.");
}
}
相关文章推荐
- 调用Thread类的方法:public final String getName() 为什么得到的线程对象的名称默认是:Thread-0、Thread-1、Thread-2、...呢?
- (44)通过继承创建线程对象的例子+getName+setName+currentThread()方法应用介绍
- 创建后,在命名线程,线程是final标识的,创建时添加线程名,后,还能用final void setName(String threadName);吗?
- JavaSE8基础 ThreadGroup getName 获得线程所在线程组的名字
- 05-多线程(多线程创建的方式一-继承Thread类)1 2 06-多线程(Thread类中的方法&线程名称) 08-多线程(线程的状态)图解
- WCF X.b 操作引用了已经从 Y.b 操作导出的消息元素 [http://tempuri.org/:b]。可以通过更改方法名称或使用 OperationContractAttribute 的 Name 属性更改其中一个操作的名称...
- Java并发04:Thread的基本方法(1)-Name、ThreadGroup、activeCount、isAlive、守护线程等
- java中不利用get方法获得私有属性、不利用set方法修改私有属性、通过执行私有方法getName获得私有属性、通过执行私有方法setName更改私有属性name的值
- [Java] Thread 创建线程的第二种方法
- 创建Thread对象时this.getName()和Thread.currentThread().getName()的差异
- JS批量获取class命名节点的方法: getElementsByClassName()
- 创建线程的方法 Thread Runnable
- Thread 和 Runnable创建新线程的区别,Runnable可以共享线程类的实例属性
- Java Concurrency - ThreadFactory, 使用工厂方法创建线程
- java 创建线程的三种方法Callable,Runnable,Thread比较及用法
- 使用getCanonicalHostName方法获得主机名
- Python中线程的创建方法:派生Thread子类
- 线程的创建之继承Thread类、线程的生命周期、线程常用的方法、currentThread方法详解
- java线程学习-Thread.currentTread().getName()和this.getName()的区别
- 2-5-Java多线程-创建线程的Runnable接口方法及Thread源码解析