Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置线程的名称?_Java_Android_Multithreading_Debugging - Fatal编程技术网

Java 如何设置线程的名称?

Java 如何设置线程的名称?,java,android,multithreading,debugging,Java,Android,Multithreading,Debugging,有没有办法在代码中为线程设置友好名称 例如,我希望图像上名为thread-11的线程被命名为类似“MyImportThread”的名称 该类有一个方法: public final void setName (String threadName) Since: API Level 1 Sets the name of the Thread. 您试过了吗?检查,有一些带有字符串名的参数。或者您可以在现有线程上调用setName(String)。您尝试过类似的方法吗 Thread.currentT

有没有办法在代码中为线程设置友好名称

例如,我希望图像上名为thread-11的线程被命名为类似“MyImportThread”的名称

该类有一个方法:

public final void setName (String threadName)

Since: API Level 1
Sets the name of the Thread.

您试过了吗?

检查,有一些带有
字符串名的参数。或者您可以在现有线程上调用
setName(String)

您尝试过类似的方法吗

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

请特别关注构造函数。

您可以轻松地在其构造函数中传递线程名称,如:

threadfoo=新线程(“foo”)

。。。或者通过调用
线程#setName

公共最终void集合名(字符串threadName)

设置线程的名称


as
thread.setName(“thread-11”)或类似的
Thread.currentThread().setName(“Thread-11”)

是的,您可以使用以下方法为线程设置名称:

Thread.getCurrentThread().setName(threadName);
试试这个:

Thread thread = new Thread("MyImportThread") {
      public void run(){    
        // code
      }
   };
   thread.start();
   System.out.println(thread.getName());

是的。。这是一个非常愚蠢的问题,但我对如何做有点困惑…:)thx对于答案,我甚至不知道他们中的哪一个是正确的:)