See the Tutorial List

Thread class and its Object - Python Multithreading


In the threading module the most popular and the most used call is the Thread class, which is primarily used to create and run threads. Thread class provides all the major functionalities required to create and manage a thread.

Thread objects are the objects of the Thread class where each object represents an activity to be performed in a separate thread of control.

There are two ways to create the Thread object and specify the activity to be performed:

  • by passing a callable object to the constructor
  • or, by overriding the run() method in a subclass.

Thread object which is created using constructor or run method can be started by using start() method. Whenever a Thread object starts a new thread then internally it's run() method is invoked.

Here is a simple example:

In the example above, we have also used the time module to make one of the thread sleep.

Basic syntax of the Thread class constructor is:

Thread(group=None, target=None, name=None, args=(), kwargs={})

We will explain the arguments of the Thread class constructor in the section below.


How Thread works?

Once we initialize a thread using the Thread class constructor, we must call its start() method to start the thread.

When the thread starts, the thread is considered alive and active. When its run() method terminates, either normally, or due to an unhandled exception then the thread stops being alive or active. The isAlive() method tests whether the thread is alive or not at any given point of time.

Other threads can call a thread's join() method to join any thread. This blocks the calling thread until the thread whose join() method is called is terminated.

For example, in the code above, from the main thread, we call t1.join() and t2.join(), hence the main thread will wait for the threads t1 and t2 to terminate and then end.

Every thread has a name associated with it. The name can be passed to the constructor, or we can set or retrieve name by using setname() and getname() methods respectively.

A flag daemon thread can be associated with any thread. The significance of this flag is that the entire python program exits when only daemon threads are left. The flag can be set or retrieved by using setDaemon() method and getDaemon() method respectively.

The main thread object corresponds to the initial thread of control in the python program. It is not a daemon thread.

Functions and Constructor in the Thread class

Now that we have seen a basic threading program with threads running, it's time to understand the code along with exploring all the important methods provided by the Thread class.


Thread class Constructor

Following is the basic syntax of the Thread class constructor:

Thread(group=None, target=None, name=None, args=(), kwargs={})

The constructor allows many arguments, some of which are required while some are not. Let's see what they are:

  • group: Should be None. It is reserved for future extension.

  • target: This is the callable object or task to be invoked by the run() method. As you can see in the code example at the top, we have specified the function names thread1 and thread2 as the value for this argument. It's default value is None.

  • name: This is used to specify the thread name. By default, a unique name is generated following the format Thread-N, where N is a small decimal number.

  • args: This is the argument tuple for the target invocation. We can provide values in it which can be used in the traget method. It's default value is empty, i.e. ()

  • kwargs: This is keyword argument dictionary for the target invocation. This defaults to {}.


start() method

This method is used to start the thread's activity. When we call this method, internally the run() method is invoked which executes the target function or the callable object.


run() method

Method representing the thread's activity.

You may override this method in a subclass extending the Thread class of the threading module. The standard run() method invokes the callable object passed to the object's constructor as the target argument with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

Here we have a simple example, where we have created a subclass in which we will override the run() method.


join([timeout]) method

When we call this method for any thread, it blocks the calling thread until the thread whose join() method is called terminates, either normally or through an unhandled exception.

If you want to provide the timeout argument, it should be a floating point number.


getName() method

This method is used to return the thread's name.


setName(name) method

Used for setting the thread's name. The name is a string used for identification purposes only.


isAlive() method

This method returns whether the thread is alive or not. A thread is alive from the moment the start() method returns until its run() method terminates.


isDaemon() method

This method is used to get the value of the thread's daemon flag.


setDaemon(daemonic) method

This method is used to set the thread's daemon flag to the Boolean value daemonic. This must be called before start() method is called.

The entire Python program exits when no active non-daemon threads are left.