Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Naming a thread and current thread in Java

Threading is a lightweight process. Thread creates and exists in the process with fewer resources; thread shares process resources. The main thread of Java is the thread that is launched when the program is launched.


There are two ways to set the name, either directly or indirectly, which we will be peeking through.

  • Creating the thread and passing its name (Direct method)
  • Using the Thread class's setName() method (Indirect Method)




Example of Creating the thread and passing its name (Direct method) in Java


In Java, threads can be named using a direct method. Without providing names, threads automatically receive default names like Thread-0, Thread-1, Thread-2, and so forth. However, Java provides a couple of techniques to modify the thread name. These techniques are available in the java.lang package, specifically in the thread class.Java enables us to set the thread name at the time of thread creation rather than passing it as an argument.


import java.io.*;
class ThreadName extends Thread {
	ThreadName(String name){
        super(name); // call to constructor of thread class as super keyword of parent class
    }
    @Override 
    public void run(){
        System.out.println("Thread is running");
    }
}
public class Main { 
	public static void main(String args[]){
		ThreadName threadName1 = new ThreadName("I Love India");
		ThreadName threadName2 = new ThreadName("India is Great");
        System.out.println("Thread one: " + threadName1.getName());
        System.out.println("Thread two: " + threadName2.getName()); 
        threadName1.start();
        threadName2.start();
   }
}




Output:

Thread one: I Love India
Thread two: India is Great
Thread is running
Thread is running 

Example of Creating the thread using the Thread class's setName() method (Indirect Method) in Java


By using the setName method on that thread object, we can set (modify) the thread's name. It will alter a thread's name.


import java.io.*;
class ThreadName extends Thread {
    @Override 
    public void run(){
        System.out.println("Thread is running");
    }
}
public class Main { 
	public static void main(String args[]){
		ThreadName threadName1 = new ThreadName();
		ThreadName threadName2 = new ThreadName();
        System.out.println("Thread one: " + threadName1.getName());
        System.out.println("Thread two: " + threadName2.getName()); 
        threadName1.start();
        threadName2.start();

        threadName1.setName("I Love India");
        threadName2.setName("India is Great");

        System.out.println("Changed name of thread : ");

        System.out.println("New Name of Thread 1 :  "+ threadName1.getName());
        System.out.println("New Name of Thread 2 : "+ threadName2.getName());
   }
}



Output:

Thread one: Thread-0
Thread two: Thread-1
Changed name of thread : 
New Name of Thread 1 :  I Love India
New Name of Thread 2 : India is Great
Thread is running
Thread is running 

Example of How to fetch the name of the current thread in Java


import java.io.*;
class ThreadName extends Thread {
    @Override 
    public void run(){
        System.out.println(Thread.currentThread().getName());
    }
}
public class Main { 
   public static void main(String args[]){
    ThreadName threadName1 = new ThreadName();
    ThreadName threadName2 = new ThreadName();
    threadName1.start();
    threadName2.start();
    threadName1.setName("I Love India");
    threadName2.setName("India is Great");
   }
}



Output:

I Love India
India is Great 

Thread Priority in java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.