Page 155 - CTS - CSA TP - Volume 2
P. 155
COMPUTER SOFTWARE APPLICATION - CITS
Step 2: Create a Thread Object
• A new instance of the Thread class is created.
• The constructor Thread(String name) is used to create the thread with the specified name (“My first thread”
in this case).
Step 3: Start the Thread
• The start() method is called on the thread object.
• This method initiates the execution of the thread and moves it to the active state.
• The thread will execute its run method in a separate thread of control.
Step 4: Get and Print Thread Name
• The getName() method is called on the thread object (t) to retrieve its name.
• The retrieved name is stored in the variable str.
• The name is then printed to the console using System.out.println().
Step 5: Execution and Output
• When you run this program, you will see the output as the name of the thread:
This program demonstrates creating a thread, starting it, and retrieving its name using the Thread class in Java.
140
CITS : IT & ITES - Computer Software Application - Exercise 101