🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Thread.getName() method in Java is used to retrieve the name of a thread.
Table of Contents
- Introduction
getName()Method Syntax- Examples
- Basic Usage
- Setting and Getting Thread Name
- Using
getName()in Multi-threaded Environment
- Real-World Use Case
- Conclusion
Introduction
The Thread.getName() method is a member of the Thread class that returns the name of the thread. Thread names are useful for identifying and distinguishing between different threads, especially in multi-threaded applications.
getName() Method Syntax
The syntax for the getName() method is as follows:
public String getName()
Returns:
- The name of the thread.
Examples
Basic Usage
To demonstrate the basic usage of getName(), we will create a thread and retrieve its name.
Example
public class GetNameExample {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
String threadName = currentThread.getName();
System.out.println("Current thread name: " + threadName);
}
}
Output:
Current thread name: main
Setting and Getting Thread Name
You can set a thread's name when creating it or by using the setName(String name) method, and then retrieve the name using getName().
Example
public class SetAndGetNameExample {
public static void main(String[] args) {
Runnable task = () -> {
Thread currentThread = Thread.currentThread();
System.out.println("Thread name: " + currentThread.getName());
};
Thread thread = new Thread(task);
thread.setName("CustomThread");
thread.start();
}
}
Output:
Thread name: CustomThread
Using getName() in Multi-threaded Environment
In a multi-threaded environment, you can use the getName() method to identify different threads.
Example
public class MultiThreadGetNameExample {
public static void main(String[] args) {
Runnable task = () -> {
Thread currentThread = Thread.currentThread();
System.out.println("Current thread: " + currentThread.getName() + " is running");
};
Thread thread1 = new Thread(task, "Thread-1");
Thread thread2 = new Thread(task, "Thread-2");
thread1.start();
thread2.start();
}
}
Output:
Current thread: Thread-1 is running
Current thread: Thread-2 is running
(Note: The output may vary in order due to the nature of multi-threading.)
Real-World Use Case
Logging and Monitoring
In a real-world scenario, the getName() method can be used for logging and monitoring purposes. By including thread names in log messages, you can better understand the flow of execution and identify issues in multi-threaded applications.
Example
public class LoggingExample {
public static void main(String[] args) {
Runnable task = () -> {
Thread currentThread = Thread.currentThread();
log("Task started in thread: " + currentThread.getName());
try {
Thread.sleep(1000); // Simulate work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
log("Task completed in thread: " + currentThread.getName());
};
Thread thread1 = new Thread(task, "Worker-1");
Thread thread2 = new Thread(task, "Worker-2");
thread1.start();
thread2.start();
}
private static void log(String message) {
System.out.println(message);
}
}
Output:
Task started in thread: Worker-1
Task started in thread: Worker-2
Task completed in thread: Worker-1
Task completed in thread: Worker-2
Conclusion
The Thread.getName() method in Java provides a way to retrieve the name of a thread. By understanding how to use this method, you can identify and manage threads in your Java applications. Whether you are working with single-threaded or multi-threaded environments, the getName() method offers used for interacting with threads and enhancing logging and monitoring capabilities in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment