简体   繁体   English

具有相同名称的线程

[英]threads having the same name

i have read one thing about java thread in java thread model api. 我在java线程模型api中读过有关java线程的一件事。 It says that more than one threads can have the same name. 它说多个线程可以具有相同的名称。 but the pid of each thread will be unique, right? 但是每个线程的pid都是唯一的,对吧? what is the name then? 那叫什么名字? isnot the thread class name? 是不是线程类名?

what is the name then? 那叫什么名字? isnot the thread class name? 是不是线程类名?

The name is whatever you set it to be using Thread.setName . 该名称是您使用Thread.setName设置它的任何名称。 It's just for the purpose of diagnostics. 它只是为了诊断的目的。 If you don't call Thread.setName or provide the name to the constructor, the value will be determined as per the docs: 如果您不调用Thread.setName或将名称提供给构造函数,则将根据文档确定该值:

Allocates a new Thread object. 分配一个新的Thread对象。 This constructor has the same effect as Thread (null, null, gname) , where gname is a newly generated name. 此构造函数与Thread (null, null, gname)具有相同的效果,其中gname是新生成的名称。 Automatically generated names are of the form "Thread-"+n , where n is an integer. 自动生成的名称的格式为"Thread-"+n ,其中n是整数。

(It's obviously more useful diagnostically to give a specific thread name which indicates its purpose.) (在诊断上,给出一个指明其用途的特定线程名称显然更有用。)

When a new thread is created, it is assigned an automatically generated name which is of the form - "Thread-0" , "Thread-1" . 创建新线程时,会为其分配一个自动生成的名称,其格式为"Thread-0""Thread-1"

But, you can any time change the name of the Thread by using Thread#setName() method: - 但是,您可以随时使用Thread#setName()方法更改Thread的名称: -

Thread.currentThread().setName("ABC");

Thread.currentThread() returns the reference to current thread executing, and then you set the name of that thread to "ABC" . Thread.currentThread()返回对当前执行线程的引用,然后将该线程的名称设置为"ABC"

Nope it is a thread name, If you see a thread dump, you will know which thread is doing what exactly. 不是它是一个线程名称,如果你看到一个线程转储,你将知道哪个线程正在做什么。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM