字符串tostring_Java线程类字符串toString()方法(带示例)

字符串tostring

线程类字符串toString() (Thread Class String toString())

  • This method is available in java.lang.Thread.toString().

    此方法在java.lang.Thread.toString()中可用。

  • This method is used to return the string representation of the thread including the thread name, thread priority, thread group.

    此方法用于返回线程的字符串表示形式,包括线程名称,线程优先级,线程组。

  • This method returns a string representation of the thread that is easy to understand for end-user.

    此方法返回线程的字符串表示形式,对于最终用户来说很容易理解。

  • This method does not return an exception.

    此方法不返回异常。

Syntax:

句法:

    String toString(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the Thread.

我们不会在Thread方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is String, this method returns String representation of the thread.

该方法的返回类型为String ,此方法返回线程的String表示形式。

Java程序演示toString()方法的示例 (Java program to demonstrate example of toString() 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 MyThread extends Thread {
    // Override run() method of Thread class
    public void run() {
        System.out.println("Thread Priority:" + Thread.currentThread().getPriority());
        System.out.println("Thread Name :" + Thread.currentThread().getName());
        System.out.println("Thread Group :" + Thread.currentThread().getThreadGroup());
    }
}

class Main {
    public static void main(String[] args) {
        // Creating an object of MyThread and calling start() of Thread class
        MyThread mt = new MyThread();
        mt.start();

        System.out.println("String representation of MyThread" + mt.toString());
    }
}

Output

输出量

E:\Programs>javac Main.java

E:\Programs>java Main
String representation of MyThreadThread[Thread-0,5,main]
Thread Priority:5
Thread Name :Thread-0
Thread Group :java.lang.ThreadGroup[name=main,maxpri=10]


翻译自: https://www.includehelp.com/java/thread-class-string-tostring-method-with-example.aspx

字符串tostring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值