Thread class in python is used to define and control an activity. Each of these activities are parts of the program that are executed concurrently in separate threads. In order to create a thread:
- Create a sub class of the thread class.
- Override the __init__ method. This method is used to initialize the data specific to a thread.
- Override the run method. This method defines the behavior of the thread.
The activity of a thread can be launched by calling start() method on the thread object. This method call moves the thread to runnable state, though the thread might not actually be running. A thread stops when it’s run method is terminated.
The thread on which the the main python program runs is called the main thread object. All the thread created are children of the main thread.
Method calls associated with thread object
Each thread can be considered to be an object, that has various methods linked to it, that can perform certain definite action. The thread class defines following methods particular to a thread that can be invoked on them:
- start(): Start the thread’s activity.
Description: It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.
- name: A string used for identification purposes only.
Description: It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.
- run(): Method representing the thread’s activity.
Description: You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
- getName(): Returns the name of the current thread.
- setName(): Sets a name for the current thread.
- is_alive(): Return whether the thread is alive.
Description: This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.
- daemon: A boolean value indicating whether this thread is a daemon thread (True) or not (False).
Description: This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when no alive non-daemon threads are left.
- isDaemon(): Returns a boolean value indicating whether the current thread is daemon or not.
- ident: The ‘thread identifier’ of this thread or None if the thread has not been started.
Description: This is a nonzero integer. See the _thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.
- join(timeout=None): TWait until the thread terminates.
Description: This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.
Python program to demonstrate thread objects
MainThread 80 GeeksforGeeks Identifier of current thread: 139778220697344 85 GFG Identifier of current thread: 139778212304640 Is thread1 alive?: False Is thread2 alive?: False Is thread1 a daemon?: False 80 GeeksforGeeks Identifier of current thread: 139778244499264 MainThread GFG_Thread Exiting the main thread
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Top 10 Programming Languages of the World – 2019 to begin with…
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Server side and Client side Programming
- Website Blocker Using Python
- Python | 404 Error handling in Flask
- Create a Website Alarm Using Python
- Generate a graph using Dictionary in Python
- Creating a Proxy Webserver in Python | Set 2
- Creating a Proxy Webserver in Python | Set 1
- Python | Parse a website with regex and urllib
- Fetching text from Wikipedia's Infobox in Python
- Pytube | Python library to download youtube videos