Methods of Thread class in Python

Methods of Thread class in Python

In this article, I am going to discuss Important Methods of Thread class in Python with examples. Please read our previous article where we discussed Multithreading in Python. As part of this article, we are going to discuss the following important methods of Thread class in detail.

  1. active_count()
  2. enumerate()
  3. isAlive()
  4. join()
  5. join(seconds)
active_count() method:

This function returns the number of active threads currently running.

Program: active_count() method of Thread class in Python (demo9.py)

from threading import *
import time

def display():
   print(current_thread().getName(), "...started")
   time.sleep(3)
   print(current_thread().getName(), "...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:

active_count() method of Thread class in Python

Initially, the number of active threads is only the Main thread. Later, when we created three threads to the target, then the total thread would become four.

enumerate() function:

This function returns a list of the objects of all active threads currently running.

Program: enumerate() function of Thread class (demo10.py)

from threading import *
import time

def display():
   print(current_thread().getName(), "...started")
   time.sleep(3)
   print(current_thread().getName(), "...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:

enumerate() function of Thread class in Python

After starting the display() function to run in three threads, a total of three plus one main threads would be running at the same time. After three seconds, the sleep time, the child threads would end. So, after that time, only the main thread would be in the execution.

isAlive() method:

This method is used to check whether a thread is still executing or not.

Program: isAlive() method in Python (demo11.py)

from threading import *
import time

def display():
   print(current_thread().getName(), "...started")
   time.sleep(3)
   print(current_thread().getName(), "...ended")

print("The Number of active Threads:", active_count())
t1=Thread(target=display, name="ChildThread1")
t2=Thread(target=display, name="ChildThread2")

t1.start()
t2.start()

print(t1.name,"is Alive :",t1.isAlive())
print(t2.name,"is Alive :",t2.isAlive())

time.sleep(5)

print(t1.name,"is Alive :",t1.isAlive())
print(t2.name,"is Alive :",t2.isAlive())

Output:

isAlive() method of Thread class in Python

join() method:

If we want a particular thread to wait for another thread to complete, then we can use join() method.

Program: join() method of Thread class (demo12.py)

from threading import *
import time

def display():
   for i in range(5):
       print("First Thread")
t=Thread(target=display, name="ChildThread")
t.start()
t.join() #This is executed by Main Thread

for i in range(5):
   print("Second Thread")

Output:

join() method of Thread class in Python

Here the main thread will wait until the child thread gets completed.

join(seconds) method:

The join method above can also be used by passing the argument ,in the seconds, time period also. In this case thread will wait only specified amount of time.

Program: join(seconds) method in Python (demo13.py)

from threading import *
import time

def display():
   for i in range(5):
       print("First Thread")
       time.sleep(2)
t=Thread(target=display, name="ChildThread")
t.start()
t.join(5) #This is executed by Main Thread

for i in range(5):
   print("Second Thread")

Output:

join(seconds) method of Thread class in Python

In the next article, I am going to discuss Daemon threads in Python. Here, in this article, I try to explain Important Methods of Thread class in Python with Examples. I hope you enjoy this Methods of Thread class in Python with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.