In order to set the custom name for a thread, you can make use of setName(String name) method,
Example:/**
* Java Code Example: code2care.org
*
* Setting Thread Name
* Thread.currentThread().getName()
*
*/
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() {
Thread.currentThread().setName("MyThread1");
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
});
//Thread using Lambda expression
Thread thread2 = new Thread(() -> {
Thread.currentThread().setName("MyThread2");
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() {
Thread.currentThread().setName("MyThread4");
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
},"MyCustomThreadName"); //run() will override this name
thread1.start();
thread2.start();
thread3.start();
thread4.start();
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
@Override
public void run() {
Thread.currentThread().setName("MyThread3");
System.out.println("Thread Name: " + 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:
- Android : Duplicate registration for activity com.example.abc - Android
- How to Open Finder using Mac Terminal - MacOS
- Must Know Homebrew Commands for Mac/Linux Users - MacOS
- Display Era details (AD BC) in Java Date using SimpleDateFormat - Java
- How to remove blank lines from a file using Notepad++ - NotepadPlusPlus
- 0xCAA20003: You ran into an authorization problem. [Microsoft] - Microsoft
- What is Microsoft 365 Message You are using more licenses on your trial than what you will purchase once the free trial ends. - Microsoft
- Java SE JDBC Select Statement Example - Java