1 # -*- coding:utf-8 -*- 2 # Author:Sure Feng 3 4 5 import threading 6 import time 7 8 9 num = 0 10 11 12 def sing(): 13 # 线程间共享全局变量,可能导致数据混乱 14 global num 15 for i in range(5000): 16 num += 1 17 print("--第【%d】次sing----" % num) 18 # time.sleep(0.1) 19 20 21 class Dance(threading.Thread): 22 def run(self): 23 global num 24 for i in range(5000): 25 num += 1 26 print("---第【%d】次dance---" % num) 27 # time.sleep(0.1) 28 29 30 def main(): 31 # 调用线程的第一种方法 32 t1 = threading.Thread(target=sing) 33 # 调用线程的第二种方法 34 t2 = Dance() 35 t1.start() 36 t2.start() 37 38 while True: 39 # 查看当前线程数量 40 threading_num = len(threading._enumerate()) 41 print("当前的线程数量为%d" % threading_num) 42 if threading_num <= 1: 43 break 44 45 # time.sleep(0.3) 46 47 if __name__ == "__main__": 48 main()