23 lines
749 B
Java
23 lines
749 B
Java
package lecture.multithreading.basics;
|
|
|
|
public class ThreadNamingExample {
|
|
public static void main(String[] args) {
|
|
// Create a thread without a specified name
|
|
Thread thread1 = new MyThread();
|
|
|
|
// Set thread name
|
|
thread1.setName("Worker-1");
|
|
|
|
// Get and print the name of the thread
|
|
System.out.println("Thread name before start: " + thread1.getName());
|
|
thread1.start();
|
|
|
|
// Main thread name
|
|
Thread mainThread = Thread.currentThread();
|
|
System.out.println("main thread name: " + mainThread.getName());
|
|
|
|
// Rename the main thread
|
|
mainThread.setName("Main-Thread");
|
|
System.out.println("New name of main thread: " + mainThread.getName());
|
|
}
|
|
} |