Page 165 - CTS - CSA TP - Volume 2
P. 165
COMPUTER SOFTWARE APPLICATION - CITS
4. Output:
• When the program is executed:
• The thread starts its execution and prints “Thread is running...”.
• Meanwhile, in the main thread, isAlive() method is called on isAliveThread to check if the thread is alive.
Since the thread is just started, it is alive.
• Therefore, “Is thread alive? true” is printed by the main thread.
5. Exception Handling:
• No explicit exception handling is performed in this program.
This program demonstrates how to use the isAlive() method to determine if a thread is currently alive or
has completed its execution. It’s often used in scenarios where you need to check the status of threads in a
multithreaded application.
Output:
4 setName(String name) Method:
• Purpose: Changes the name of the thread.
• Syntax:setName(String name)
• Parameters:
• name: The new name to be assigned to the thread.
• Use Case:
• Provides a way to give a meaningful and recognizable name to threads for better identification.
• Useful for debugging and logging purposes.
class SetNameExample extends Thread {
public void run() {
System.out.println(“Thread is running...”);
}
public static void main(String args[]) {
SetNameExample setNameThread = new SetNameExample();
setNameThread.setName(“CustomThreadName”);
setNameThread.start();
System.out.println(“Thread Name: “ + setNameThread.getName());
}
}
150
CITS : IT & ITES - Computer Software Application - Exercise 104