Saturday 12 December 2015

Python: Get current thread object

‘threading.current_thread()’ method return the current thread object, corresponding to the caller’s thread of control.

MyThread.py
import threading
import time

class MyThread(threading.Thread):
 def run(self):
  print(threading.current_thread().getName(), " Finished")

thread1 = MyThread(name="Thread_1")
thread2 = MyThread(name="Thread_2")
thread3 = MyThread(name="Thread_3")

thread1.start()
thread2.start()
thread3.start()

$ python3 MyThread.py 
Thread_1  Finished
Thread_2  Finished
Thread_3  Finished


Previous                                                 Next                                                 Home

No comments:

Post a Comment