getName() and setName()
- Every thread in python has name. It may be default name generated by python or customized name provided by programmer. We can get and set name of thread by using the following thread class methods.
- t.getName() ⇒ Returns name of thread.
- t.setName(newName) ⇒ Set our own name.
Note
- getName() and setName() function give ‘DeprecationWarning’. In the place of this we recommend to use implicit variable ‘name’ and ‘set’ respectively.
Example
Python
from threading import *
print(current_thread().name)
current_thread().setName("Our own thread")
print(current_thread().name)
print(current_thread().name)
Output
PowerShell
MainThread
Our own thread
Our own thread
ident
- For every thread internally a unique identification number is available. We can access this id by using implicit variable ‘ident’.
Example
Python
from threading import *
def test():
print("Child Thread")
t=Thread(target=test)
t.start()
print("Main Thread Identification Number:",current_thread().ident)
print("Child Thread Identification Number:",t.ident)
Output
PowerShell
Child Thread
Main Thread Identification Number: 29836
Child Thread Identification Number: 15404
active_count()
- This function returns the number of active threads currently running.
Example
Python
from threading import *
import time
def display():
print(current_thread().name,"...started")
time.sleep(3)
print(current_thread().name,"...ended")
print("The Number of active Threads:",active_count())
t1=Thread(target=display,name="ChildThread1")
t2=Thread(target=display,name="ChildThread2")
t3=Thread(target=display,name="ChildThread3")
t1.start()
t2.start()
t3.start()
print("The Number of active Threads:",active_count())
time.sleep(5)
print("The Number of active Threads:",active_count())
Output
PowerShell
The Number of active Threads: 1
ChildThread2 ...started
ChildThread1 ...started
ChildThread3 ...started
The Number of active Threads: 4
ChildThread2 ...ended
ChildThread1 ...ended
ChildThread3 ...ended
The Number of active Threads: 1
enumerate()
- This function returns a list of all active threads currently running.
Example
Python
from threading import *
import time
def display():
print(current_thread().name,"...started")
time.sleep(3)
print(current_thread().name,"...ended")
t1=Thread(target=display,name="ChildThread1")
t2=Thread(target=display,name="ChildThread2")
t3=Thread(target=display,name="ChildThread3")
t1.start()
t2.start()
t3.start()
l=enumerate()
for t in l:
print("Thread Name:",t.name)
time.sleep(5)
l=enumerate()
for t in l:
print("Thread Name:",t.name)
Output
PowerShell
Thread Name: MainThread
Thread Name: ChildThread1
Thread Name: ChildThread2
Thread Name: ChildThread3
ChildThread2 ...started
ChildThread3 ...started
ChildThread1 ...started
ChildThread3 ...ended
ChildThread2 ...ended
ChildThread1 ...ended
Thread Name: MainThread
is_alive()
- This method checks whether a thread is still executing or not.
Example
Python
from threading import *
import time
def display():
print(current_thread().name,"...started")
time.sleep(3)
print(current_thread().name,"...ended")
t1=Thread(target=display,name="ChildThread1")
t2=Thread(target=display,name="ChildThread2")
t1.start()
t2.start()
print(t1.name,"is Alive :",t1.is_alive())
print(t2.name,"is Alive :",t2.is_alive())
time.sleep(5)
print(t1.name,"is Alive :",t1.is_alive())
print(t2.name,"is Alive :",t2.is_alive())
Output
PowerShell
ChildThread1 ...started
ChildThread2 ...started
ChildThread1 is Alive : True
ChildThread2 is Alive : True
ChildThread1 ...ended
ChildThread2 ...ended
ChildThread1 is Alive : False
ChildThread2 is Alive : False
join ()
- If a thread wants to wait until completing some other thread then we should go for join() method.
Example
Python
from threading import *
import time
def display():
for i in range(5):
print("Seetha Thread")
time.sleep(2)
t=Thread(target=display)
t.start()
t.join()
for i in range(5):
print("Rama Thread")
Output
PowerShell
Seetha Thread
Seetha Thread
Seetha Thread
Seetha Thread
Seetha Thread
Rama Thread
Rama Thread
Rama Thread
Rama Thread
Rama Thread
join(seconds)
- In this case thread will wait only specified amount of time.
Example
Python
from threading import *
import time
def display():
for i in range(5):
print("Seetha Thread")
time.sleep(2)
t=Thread(target=display)
t.start()
t.join(5)
for i in range(5):
print("Rama Thread")
Output
PowerShell
Seetha Thread
Seetha Thread
Seetha Thread
Rama Thread
Rama Thread
Rama Thread
Rama Thread
Rama Thread
Seetha Thread
Seetha Thread
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!