ADVERTISEMENT
ADVERTISEMENT

Python Thread Class | getName() Method with Example

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

Python Thread.getName() Method

Thread.getName() method is an inbuilt method of the Thread class of the threading module in Python. This method is used to get the name of the thread.

Module:

    from threading import Thread

Syntax:

    getName()

Parameter(s):

  • None

Return value:

The return type of this method is <class 'str'>, it returns thread name of the calling object.

Example:

# Python program to explain the
# use of getName() method

import time
import threading

def thread_1(i):
    time.sleep(5)
    print('Value by '+ str(threading.current_thread().getName())+" is: ", i)

def thread_2(i):
    print('Value by '+ str(threading.current_thread().getName())+" is: ", i)
    
def thread_3(i):
    time.sleep(4)
    print('Value by '+ str(threading.current_thread().getName())+" is: ", i)
    
# Creating three sample threads 
thread1 = threading.Thread(target=thread_1, args=(10,))
thread2 = threading.Thread(target=thread_2, args=(20,))
thread3 = threading.Thread(target=thread_2, args=(30,))

# Running the threads
thread1.start()
thread2.start()
thread3.start()

Output

Value by Thread-2 is:  20
Value by Thread-3 is:  30
Value by Thread-1 is:  10
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.