Syntax:
public String getName()
public void setName(String name)
class Coderz extends Thread
{
public void run()
{
System.out.println("running...");
}
public static void main(String args[])
{
// creating two threads
// using the contructor of the class
Coderz ob1=new Coderz();
Coderz ob2=new Coderz();
// invoking the getName() method to get the names
System.out.println("Name of ob1:"+ob1.getName());
System.out.println("Name of ob2:"+ob2.getName());
// invoking the start() method on both the threads
ob1.start();
ob2.start();
ob1.setName("Coderzy java multithreading");
System.out.println("After changing name of ob1:"+ob1.getName());
}
}
Name of ob1:Thread-0
Name of ob2:Thread-1
running...
running...
After changing name of ob1:Coderzy java multithreading
import java.io.*;
// The ThreadNameClass is the child class of the class Thread
class ThreadName extends Thread
{
// constructor of the class
ThreadName(String threadName)
{
// invoking the constructor of
// the superclass, which is Thread class.
super(threadName);
}
// overriding the method run()
public void run()
{
System.out.println(" The thread is running....");
}
}
public class ThreadNamingExample
{
// main method
public static void main (String argvs[])
{
// creating two threads and settting their name
// using the contructor of the class
ThreadName th1 = new ThreadName("Coderz1");
ThreadName th2 = new ThreadName("Coderz2");
// invoking the getName() method to get the names
// of the thread created above
System.out.println("Thread - 1: " + th1.getName());
System.out.println("Thread - 2: " + th2.getName());
// invoking the start() method on both the threads
th1.start();
th2.start();
}
}
Thread - 1: Coderz1
Thread - 2: Coderz2
The thread is running....
The thread is running....
A reference to the thread that is currently running is returned by the currentThread() method.
public static Thread currentThread()
public class Coderz extends Thread
{
public void run()
{
// Getting the current thread name
// using getname() method
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[])
{
// Creating two threads inside main() method
Coderz t1=new Coderz();
Coderz t2=new Coderz();
t1.start();
t2.start();
}
}
Thread-0
Thread-1
Note: also read about the Thread.sleep() method in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…