java setname_Java线程类最终void setName(String thread_name)方法,带示例

java setname

线程类最终void setName(String thread_name) (Thread Class final void setName(String thread_name))

  • This method is available in package java.lang.Thread.setName(String thread_name).

    包java.lang.Thread.setName(String thread_name)中提供了此方法。

  • This method is used to sets the name of this thread.

    此方法用于设置此线程的名称。

  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.

    此方法不是静态的,因此该方法可通过Thread类对象访问,而无法通过类名称访问。

  • This method is final so we can't override this method in our program.

    此方法是最终方法,因此我们无法在程序中覆盖此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method does not raise an exception if this thread can't modify.

    如果此线程无法修改,则此方法不会引发异常。

Syntax:

句法:

    final void setName(String thread_name){
    }

Parameter(s):

参数:

We pass only one object as a parameter in the method of the Thread and the parameter is the new name of this thread.

我们仅在Thread方法中将一个对象作为参数传递,并且参数是该线程的新名称。

Return value:

返回值:

The return type of this method is String, it returns the name of this thread as a string.

该方法的返回类型为String ,它以字符串形式返回此线程的名称。

Java程序演示setName()方法的示例 (Java program to demonstrate example of setName() method)

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class SetThreadName extends Thread {
    // Override run() of Thread class
    public void run() {
        System.out.println("The priority of this thread is : " + Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        // Creating an object of SetThreadName class
        SetThreadName st_name = new SetThreadName();

        System.out.println("The old name of this thread is " + " " + st_name.getName());

        // We are setting the name of the thread SetThreadName
        st_name.setName("SetThreadName");

        /*  Calling start() method with SetThreadName class 
            object of Thread class
        */
        st_name.start();

        /*  By using getName() method to return the name of 
            this thread [SetThreadName ]
        */
        System.out.println("The new name of this thread is " + " " + st_name.getName());
    }
}

Output

输出量

E:\Programs>javac SetThreadName.java

E:\Programs>java SetThreadName
The old name of this thread is  Thread-0
The new name of this thread is  SetThreadName
The priority of this thread is : 5


翻译自: https://www.includehelp.com/java/thread-class-final-void-setname-string-thread_name-method-with-example.aspx

java setname

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值