java给线程命名
Each thread in Java has its own name which is set by the JVM by default. Although there are many other attributes associated to the thread like: id, priority etc.
Java中的每个线程都有其自己的名称,该名称由JVM默认设置。 尽管还有许多其他与线程相关的属性,例如:id,priority等。
we can get name of a thread by calling getName()
method of Thread class. If we wish to set new name of the thread then setName() method can be used. Both methods belongs to Thread class and even we can set name of thread by passing into constructor during creating object. Lets understand by the examples.
我们可以通过调用Thread类的getName()
方法来获得线程的名称。 如果我们希望设置线程的新名称,则可以使用setName()方法。 这两个方法都属于Thread类,甚至我们可以在创建对象时通过传递给构造函数来设置线程的名称。 让我们通过示例来理解。
这两种方法的语法如下。 (Syntax of the two methods is given below.)
setName(String name)
getName()
示例:获取线程名称 ( Example: Fetch Thread Name )
lets first fetch the thread name set by the JVM. It is default name so initially we ca