Java Thread Set Name Example

Introduction

In Java, each thread has a name for identification purposes. By default, the Java Virtual Machine (JVM) assigns a name to each thread (e.g., "Thread-0", "Thread-1"), but you can set a custom name for a thread. Setting a thread's name can be useful for debugging and monitoring purposes, as it makes it easier to identify and distinguish between different threads in a multi-threaded application.

Table of Contents

  1. Setting the Thread Name
  2. Getting the Thread Name
  3. Example: Setting and Getting Thread Names
  4. Using Lambda Expressions to Set Thread Names
  5. Conclusion

1. Setting the Thread Name

You can set a thread's name by using the setName(String name) method of the Thread class. This method takes a single argument, which is the new name for the thread.

Syntax:

public void setName(String name)

2. Getting the Thread Name

You can get a thread's name by using the getName() method of the Thread class. This method returns the name of the thread as a String.

Syntax:

public String getName()

3. Example: Setting and Getting Thread Names

Let's create an example where we set and get the name of a thread.

Example:

class MyThread extends Thread {
    public MyThread(String name) {
        super(name); // Call the parent constructor to set the thread name
    }

    @Override
    public void run() {
        System.out.println("Thread is running: " + getName());
    }

    public static void main(String[] args) {
        MyThread thread1 = new MyThread("Thread-A");
        MyThread thread2 = new MyThread("Thread-B");

        thread1.start(); // Start thread1
        thread2.start(); // Start thread2

        // Change the name of thread1
        thread1.setName("Renamed-Thread-A");

        System.out.println("Thread1 new name: " + thread1.getName());
        System.out.println("Thread2 name: " + thread2.getName());
    }
}

Output:

Thread is running: Thread-A
Thread is running: Thread-B
Thread1 new name: Renamed-Thread-A
Thread2 name: Thread-B

Explanation:

  • The MyThread class extends the Thread class and calls the parent constructor to set the thread name.
  • The run method prints the name of the thread using the getName method.
  • In the main method, two MyThread objects (thread1 and thread2) are created with custom names ("Thread-A" and "Thread-B").
  • The threads are started using the start method.
  • The name of thread1 is changed to "Renamed-Thread-A" using the setName method.
  • The new names of the threads are printed using the getName method.

4. Using Lambda Expressions to Set Thread Names

You can also set thread names using lambda expressions for conciseness, especially when using the Runnable interface.

Example:

public class LambdaThreadNameExample {
    public static void main(String[] args) {
        Runnable task1 = () -> System.out.println("Thread is running: " + Thread.currentThread().getName());
        Runnable task2 = () -> System.out.println("Thread is running: " + Thread.currentThread().getName());

        Thread thread1 = new Thread(task1, "Lambda-Thread-A");
        Thread thread2 = new Thread(task2, "Lambda-Thread-B");

        thread1.start(); // Start thread1
        thread2.start(); // Start thread2

        // Change the name of thread1
        thread1.setName("Renamed-Lambda-Thread-A");

        System.out.println("Thread1 new name: " + thread1.getName());
        System.out.println("Thread2 name: " + thread2.getName());
    }
}

Output:

Thread is running: Lambda-Thread-A
Thread is running: Lambda-Thread-B
Thread1 new name: Renamed-Lambda-Thread-A
Thread2 name: Lambda-Thread-B

Explanation:

  • Lambda expressions are used to create tasks that print the current thread's name.
  • Two Thread objects (thread1 and thread2) are created with custom names ("Lambda-Thread-A" and "Lambda-Thread-B").
  • The threads are started using the start method.
  • The name of thread1 is changed to "Renamed-Lambda-Thread-A" using the setName method.
  • The new names of the threads are printed using the getName method.

5. Conclusion

Setting and getting thread names in Java is a simple yet powerful feature that helps in debugging and monitoring multi-threaded applications. By using the setName and getName methods, you can easily identify and manage threads in your programs. This guide provided examples using both the Thread class and lambda expressions to demonstrate how to set and get thread names effectively.

Happy coding!

Comments