import _thread
import threading
from threading import Thread
import time
def doSth(arg):
threadName = threading.current_thread().getName()
tid = threading.current_thread().ident
for i in range(5):
print("%s *%d @%s,tid=%d" % (arg, i, threadName, tid))
time.sleep(2)
def simpleThread():
_thread.start_new_thread(doSth, ("拍森",))
mainThreadName = threading.current_thread().getName()
print(threading.current_thread())
for i in range(5):
print("劳资是主线程@%s" % (mainThreadName))
time.sleep(1)
while True:
pass
def threadingThread():
t = threading.Thread(target=doSth, args=("大王派我来巡山",))
t.start()
class MyThread(threading.Thread):
def __init__(self, name, task, subtask):
super().__init__()
self.name = name
self.task = task
self.subtask = subtask
def run(self):
for i in range(5):
print("【%s】并【%s】 *%d @%s" % (self.task, self.subtask, i, threading.current_thread().getName()))
time.sleep(2)
def classThread():
mt = MyThread("小分队I", "巡山", "扫黄")
mt.start()
for i in range(5):
tid = threading.current_thread().ident
print("劳资是大王@%s,tid=%d" % (threading.current_thread().getName(), tid))
time.sleep(1)
print("主线程over")
def importantAPI():
print(threading.currentThread())
t1 = threading.Thread(target=doSth, args=("巡山",))
t2 = threading.Thread(target=doSth, args=("巡水",))
t3 = threading.Thread(target=doSth, args=("巡鸟",))
t1.start()
t2.start()
t3.start()
print(t1.isAlive())
print(t2.isDaemon())
print(t3.getName())
t3.setName("巡鸟")
print(t3.getName())
print(t3.ident)
tlist = threading.enumerate()
print("当前活动线程:", tlist)
count = threading.active_count()
print("当前活动线程有%d条" % (count))
if __name__ == '__main__':
importantAPI()
pass