0% found this document useful (0 votes)
6 views1 page

Docu 3

This Java program demonstrates how to create and rename threads. It defines a class 'ThreadNaming' that extends the Thread class and overrides the run() method to print a message when the thread is running. The main class 'ThreadEx' creates two threads, prints their default names, starts them, changes their names, and then prints the new names.

Uploaded by

aryakrishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Docu 3

This Java program demonstrates how to create and rename threads. It defines a class 'ThreadNaming' that extends the Thread class and overrides the run() method to print a message when the thread is running. The main class 'ThreadEx' creates two threads, prints their default names, starts them, changes their names, and then prints the new names.

Uploaded by

aryakrishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Java Program Illustrating How to Get and Change the

// Name of a Thread

// Importing input output classes


import java.io.*;

// Class 1

class ThreadNaming extends Thread {

@Override
public void run()
{

// Print statement when run() is called over thread


System.out.println("Thread is running.....");
}
}

// Class 2
// Main class
class ThreadEx {

// Main driver method


public static void main(String[] args)
{

// Creating two threads via above class


// as it is extending Thread class
ThreadNaming t1 = new ThreadNaming();
ThreadNaming t2 = new ThreadNaming();

// Fetching the above created threads names


// using getName() method
System.out.println("Thread 1: " + t1.getName());
System.out.println("Thread 2: " + t2.getName());

// Starting threads using start() method


t1.start();
t2.start();

// Now changing the name of threads


t1.setName("Neha");
t2.setName("Hari");

// Printing the above names


System.out.println("New Thread 1 name: "
+ t1.getName());
System.out.println("New Thread 2 name: "
+ t2.getName());
}
}

You might also like