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,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to Copy all text to Clipboard in Vim - vi
- Permanently Set or Change $JAVA_HOME on Mac (macOS) - MacOS
- [Tutorial] How to Customize Notepad++ Toolbar - NotepadPlusPlus
- MacoOS - xyz is an app downloaded from the internet. Are you sure you want to open it? Alert - MacOS
- Java get day of the week as an int using DayOfWeek - Java
- How to print an exception in Python - Python
- How to fix Java HTTP java.net.UnknownHostException - Java
- [git] fatal: your current branch 'main' does not have any commits yet - Git