/**
* Java Code Example: code2care.org
*
* Getting Thread Name
* Thread.currentThread().getName()
*
* Custom Thread Name
*
*/
public class ThreadExample extends Thread {
public static void main(String[] args) {
//Thread using Anonymous class
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
});
//Thread using Lambda expression
Thread thread2 = new Thread(() -> System.out.println("Thread Name: " + Thread.currentThread().getName()));
//Thread using class that extends Thread Class
ThreadExample thread3 = new ThreadExample();
//Thread using Anonymous class with custom Name
Thread thread4 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
},"MyCustomThreadName");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
@Override
public void run() {
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
}
Output:
Thread Name: Thread-1
Thread Name: Thread-2
Thread Name: main
Thread Name: MyCustomThreadName
Thread Name: Thread-0
To get the name of the current thread you can make use of Thread.currentThread().getName()
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How To Remove Only Conditional Formatting in Excel - Microsoft
- How to Set Up Dual Monitors on a Windows 11 PC - Windows-11
- How to add Back Button on Toolbar in Android [Tutorial] - Android
- Fix: nano is not recognized as an internal or external command - Windows PowerShell - Powershell
- Calculate Area and Circumference of Circle - C-Program
- Python: Fix command not found pip or pip3 on zsh shell - Python
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds - Java
- Page actions are temporarily disabled [Google Search Console Page Crawling] - Google