📜  在Java中命名线程并获取当前线程的名称

📅  最后修改于: 2022-05-13 01:54:42.570000             🧑  作者: Mango

在Java中命名线程并获取当前线程的名称

线程可以称为轻量级进程。线程使用较少的资源来创建和存在于进程中;线程共享进程资源。 Java的主线程是程序启动时启动的线程。现在让我们讨论一下我们可以用什么方式命名线程的古怪概念。

方法:我们可以通过两种方式直接或间接地设置名称,我们将通过它来窥视。

  1. 创建线程并传递线程名称(直接方法)
  2. 使用 Thread 类的 setName() 方法(间接方法)

方法一:创建线程并传递线程名称

它是Java中直接命名线程的方法,每个线程都有一个名称,即:Thread-0, Thread-1, Thread-2,….等等。 Java提供了一些方法来更改线程名称。基本上有两种方法来设置线程名称。这两种方法都在Java.lang.Thread 类中定义。

极客,现在你一定想知道如何直接设置线程的名称?在Java中,我们可以在创建线程时设置线程名称,并绕过线程名称作为参数,如下例所示:

例子:

Java
// Java Program Illustrating How to Set the name
// of Thread at time of Creation
 
// Importing I/O classes from java.io package
import java.io.*;
 
// Class 1
// Helper class
class ThreadNaming extends Thread {
 
    // Parameterized constructor
    ThreadNaming(String name)
    {
        // Call to constructor of
        // the Thread class as super keyword
        // refers to parent class
        super(name);
    }
 
    // run() method for thread
    @Override public void run()
    {
        // Print statement when thread is called inside
        // main()
        System.out.println("Thread is running.....");
    }
}
 
// Class 2
// Main class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
 
        // Creating two threads
        ThreadNaming t1 = new ThreadNaming("geek1");
        ThreadNaming t2 = new ThreadNaming("geek2");
 
        // Getting the above created threads names.
        System.out.println("Thread 1: " + t1.getName());
        System.out.println("Thread 2: " + t2.getName());
 
        // Starting threads using start() method
        t1.start();
        t2.start();
    }
}


Java
// Java Program Illustrating How to Get and Change the
// Name of a Thread
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Helper class extending Thread class
class ThreadNaming extends Thread {
 
    // run() method for thread which is called
    // as soon as start() is called over threads
    @Override public void run()
    {
 
        // Print statement when run() is called over thread
        System.out.println("Thread is running.....");
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating two threads via above class
        // as it is extending Thread class
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
 
        // Fetching the above created threads names
        // using getName() method
        System.out.println("Thread 1: " + t1.getName());
        System.out.println("Thread 2: " + t2.getName());
 
        // Starting threads using start() method
        t1.start();
        t2.start();
 
        // Now changing the name of threads
        t1.setName("geeksforgeeks");
        t2.setName("geeksquiz");
 
        // Again getting the new names of threads
        System.out.println(
            "Thread names after changing the "
            + "thread names");
 
        // Printing the above names
        System.out.println("New Thread 1 name:  "
                           + t1.getName());
        System.out.println("New Thread 2 name: "
                           + t2.getName());
    }
}


Java
// Java program to Illustrate How to Get Name of
// Current Executing thread
// Using getName() Method
 
// Importing reqiored I/O classes
import java.io.*;
 
// Class 1
// Helper class extending to Thread class
class ThreadNaming extends Thread {
 
    // run() method for this thread
    @Override public void run()
    {
        // Display message
        System.out.println(
            "Fetching current thread name..");
 
        // Getting the current thread name
        // using getname() method
        System.out.println(
            Thread.currentThread().getName());
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main method driver
    public static void main(String[] args)
    {
 
        // Creating two threads inside main() method
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
 
        // Starting threads using start() method which
        // automatically calls run() method
        t1.start();
        t2.start();
    }
}


输出
Thread 1: geek1
Thread 2: geek2
Thread is running.....
Thread is running.....

方式二:使用 Thread 类的 setName() 方法

我们可以通过调用该线程对象的 setName 方法来设置(更改)线程的名称。它将更改线程的名称。

句法:

public final void setName(String name)

参数:指定线程名称的字符串

例子:

Java

// Java Program Illustrating How to Get and Change the
// Name of a Thread
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Helper class extending Thread class
class ThreadNaming extends Thread {
 
    // run() method for thread which is called
    // as soon as start() is called over threads
    @Override public void run()
    {
 
        // Print statement when run() is called over thread
        System.out.println("Thread is running.....");
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating two threads via above class
        // as it is extending Thread class
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
 
        // Fetching the above created threads names
        // using getName() method
        System.out.println("Thread 1: " + t1.getName());
        System.out.println("Thread 2: " + t2.getName());
 
        // Starting threads using start() method
        t1.start();
        t2.start();
 
        // Now changing the name of threads
        t1.setName("geeksforgeeks");
        t2.setName("geeksquiz");
 
        // Again getting the new names of threads
        System.out.println(
            "Thread names after changing the "
            + "thread names");
 
        // Printing the above names
        System.out.println("New Thread 1 name:  "
                           + t1.getName());
        System.out.println("New Thread 2 name: "
                           + t2.getName());
    }
}
输出
Thread 1: Thread-0
Thread 2: Thread-1
Thread is running.....
Thread names after changing the thread names
New Thread 1 name:  geeksforgeeks
New Thread 2 name: geeksquiz
Thread is running.....

如何获取当前线程的名称?

现在让我们继续讨论获取当前线程的名称。我们可以在创建线程时获取当前线程名称,并绕过线程名称作为参数。

方法: currentThread()

它在Java.langThread 类中定义。

返回类型:它返回对当前正在执行的线程的引用

句法:

public static Thread currentThread()

例子:

Java

// Java program to Illustrate How to Get Name of
// Current Executing thread
// Using getName() Method
 
// Importing reqiored I/O classes
import java.io.*;
 
// Class 1
// Helper class extending to Thread class
class ThreadNaming extends Thread {
 
    // run() method for this thread
    @Override public void run()
    {
        // Display message
        System.out.println(
            "Fetching current thread name..");
 
        // Getting the current thread name
        // using getname() method
        System.out.println(
            Thread.currentThread().getName());
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main method driver
    public static void main(String[] args)
    {
 
        // Creating two threads inside main() method
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
 
        // Starting threads using start() method which
        // automatically calls run() method
        t1.start();
        t2.start();
    }
}
输出
Fetching current thread name..
Thread-0
Fetching current thread name..
Thread-1