Python threading Module | main_thread() Method with Example

Python threading.main_thread() Method: Here, we are going to learn about the getName() method of threading Module in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 14, 2020

Python threading.main_thread() Method

main_thread() is an inbuilt method of the threading module in Python. It is used to return the main Thread object. It is the thread from which the Python interpreter has started, in normal conditions.

Module:

    import threading

Syntax:

    main_thread()

Parameter(s):

  • None

Return value:

The return type of this method is <class 'threading._MainThread'>, it returns the main Thread object.

Example:

# Python program to explain the
# use of  main_thread()  method in Threading Module

import time
import threading

def thread_1(i):
    time.sleep(5)
    print("Value by Thread-:",i)
    
def thread_2(i):
    print("Value by Thread-2:",i)
    
def thread_3(i):
    time.sleep(4)
    print("Value by Thread-3:",i)
    
def thread_4(i):
    time.sleep(1)
    print("Value by Thread-4:",i)

# Creating sample threads 
thread1 = threading.Thread(target=thread_1, args=(10,))
thread2 = threading.Thread(target=thread_2, args=(20,))
thread3 = threading.Thread(target=thread_3, args=(30,))
thread4 = threading.Thread(target=thread_4, args=(50,))

print("Main thread for the given program:", threading.main_thread())

# Starting the threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()

Output

Main thread for the given program: <_MainThread(MainThread, started 140269857195776)>
Value by Thread-2: 20
Value by Thread-4: 50
Value by Thread-3: 30
Value by Thread-: 10





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.