/**
* 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,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- How to check your IP using bash for Windows? - Bash
- How to know the Docker Engine Version - Docker
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5 - jQuery
- Microsoft Sign-in Error Code: 50058 (Request Id, Correlation Id and Timestamp) - Microsoft
- Instant Run requires Tools | Android | Enable ADB integration to be enabled - Android-Studio
- Bootstrap Button Colors Classes - Bootstrap
- Java java.time.Clock class code examples [Java Date Time API] - Java
- ls command sort by file size [Linix/UNIX/macOS/bash] - Linux