ADVERTISEMENT
ADVERTISEMENT

Python Thread Class | setName() Method with Example

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

Python Thread.setName() Method

Thread.setName() method is an inbuilt method of the Thread class of the threading module in Python. It uses a Thread object and sets the name of the thread.

Module:

    from threading import Thread

Syntax:

    setName()

Parameter(s):

  • None

Return value:

The return type of this method is <class 'NoneType'>, it sets the name of the Thread object which calls this method.

Example:

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

import time
import threading

def thread_1(i):
    time.sleep(5)
    #threading.current_thread.setName("frgrfvrv")
    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,))
thread1.setName("Thread_number_1")
thread2 = threading.Thread(target=thread_2, args=(20,))
thread2.setName("Thread_number_2")
thread3 = threading.Thread(target=thread_2, args=(30,))
thread3.setName("Thread_number_3")

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

Output

Value by Thread_number_2 is:  20
Value by Thread_number_3 is:  30
Value by Thread_number_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.