In the above program, we created two classes MyThread and Main. We created MyThread class by implementing the Runnable interface.
The Main class contains a main() method. The main() method is the entry point for the program. Here, we created the 3 threads and set their names using the setName() method and get the thread name using the getName() method and printed the result.
Program/Source Code:
The source code to set a thread name using the setName() method is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two classes MyThread and Main. We created MyThread class by implementing the Runnable interface.
The Main class contains a main() method. The main() method is the entry point for the program. Here, we created the 3 threads and set their names using the setName() method and get the thread name using the getName() method and printed the result.