public class Server {
public static void main(String[] args) {
int i = 0
while (i <10) {
i++
Thread t = new Thread(new MyExecute("name" + i))
t.start()
}
}
}
class MyExecute implements Runnable {
private String name
public MyExecute(String name) {
this.name = name
}
public void run() {
Thread.currentThread().setName(this.name)
System.out.println(Thread.currentThread().getName())
}
}
每个线程都有一个标识名,多个线程可以同名。如果线程创建时没有指定标识名,当 Java 虚拟机启动时,就会为其生成一个新名称。Thread 的源码
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit>does. */
private static native void registerNatives()
static {
registerNatives()
}
private char name[]
private int priority
private Thread threadQ
private long eetop
...
public final void setName(String name) {
checkAccess()
this.name = name.toCharArray()
}
...
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)