This method sets the name of a thread.
Python threading module provides a current_thread() function which returns the reference to the current executing thread object. Using this thread object, it is possible to set and get the name of executing thread by calling setName and getName methods on the thread object respectively.
import threading
import time
def Thread1():
print ('Starting ',threading.currentThread().getName())
time.sleep(2)
print ('Exiting ',threading.currentThread().getName())
def Thread2():
print ( 'Starting ',threading.currentThread().getName())
time.sleep(3)
print ('Exiting ',threading.currentThread().getName())
t1 = threading.Thread(target=Thread2)
t2 = threading.Thread(target=Thread1)
t2.start()
t2.setName('new thread2 ')
t1.start()
t1.setName('new thread1 ')
output:
Name | Views | Likes |
---|---|---|
Python threading Timer | 890 | 0 |
Python threading getName | 423 | 0 |
Program to count "Number of Vowels" in the string | 350 | 0 |
Python threading isAlive | 1588 | 0 |
Python threading introduction | 810 | 0 |
Python threading setName | 1216 | 0 |
Python threading isDaemon | 742 | 0 |
Python threading get_ident | 470 | 0 |
Python threading currentThread | 459 | 0 |
Python threading active_count | 441 | 0 |
Python threading enumerate | 508 | 0 |
Python threading setDaemon | 1061 | 0 |
Python threading main_thread | 432 | 0 |
Python threading cancel | 504 | 0 |
Comments