标签:程序 odi bre import 方便 enumerate sleep coding art
#coding=utf-8 import threading import time def saySorry(): print("亲爱的,我错了,我能吃饭了吗?") time.sleep(1) if __name__ == "__main__": for i in range(5): t = threading.Thread(target=saySorry) t.start() #启动线程,即让线程开始执行
#coding=utf-8 import threading from time import sleep,ctime def sing(): for i in range(3): print("正在唱歌...%d"%i) sleep(1) def dance(): for i in range(3): print("正在跳舞...%d"%i) sleep(1) if __name__ == ‘__main__‘: print(‘---开始---:%s‘%ctime()) t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() #sleep(5) # 屏蔽此行代码,试试看,程序是否会立马结束? print(‘---结束---:%s‘%ctime())
#coding=utf-8 import threading from time import sleep,ctime def sing(): for i in range(3): print("正在唱歌...%d"%i) sleep(1) def dance(): for i in range(3): print("正在跳舞...%d"%i) sleep(1) if __name__ == ‘__main__‘: print(‘---开始---:%s‘%ctime()) t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() while True: length = len(threading.enumerate()) print(‘当前运行的线程数为:%d‘%length) if length<=1: break sleep(0.5)
主线程和子线程
默认存在的就是主线程,新创建出来的就是子线程。
标签:程序 odi bre import 方便 enumerate sleep coding art
原文地址:https://www.cnblogs.com/snailon/p/11468507.html