Java — Getting and Setting Name of Thread | Code Factory

  • Every thread in Java has some name, It may be default name generated by JVM or customized name provided by programmer.
/* Returns this thread's name. */
public final String getName();
/* Changes the name of this thread to be equal to the argument name. */
public final void setName(String name);
Thread.currentThread().getName();
Thread.currentThread().setName("Code");
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadName {
public static void main(String... args) {
System.out.println(Thread.currentThread().getName());
MyThread t = new MyThread();
System.out.println(t.getName());
Thread.currentThread().setName("Code");
System.out.println(Thread.currentThread().getName());
}
}
class MyThread extends Thread {
public void run() {
System.out.println("Child Thread");
}
}
main
Thread-0
Code

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Single Sign-On With WSO2 Identity Server

HTML Element

DisAtBot — How I Built a Chatbot With Telegram And Python

Go & TestContainers

How I Use Scrum To Run My Business

Product Hunt will test your architecture if you don’t

Validation: Are we building the right system?

The Epitome Heist: Introduction to Timeline in Unity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Why Build Tools Matter for a Well-Grounded Developer

How to shuffle a deck of cards

Concurrency in Java: Executor Service (Part 1)

What to look for in a Code Review