python3 多线程

code

import time
import threading
class MyCounter(threading.Thread):
    """自定义线程类型,继承threading.Thread类"""
    # 类属性
    num = 1

    def run(self):
        """重写run方法,在线程start()启动后会自动执行"""
        while True:
            MyCounter.num += 1
            print(f"{threading.current_thread().getName()} num1: {MyCounter.num}")
            time.sleep(1)


# 创建线程对象
t1 = MyCounter()
t2 = MyCounter()
# 启动线程
t1.start()
t2.start()
print("main")

原文地址:https://www.cnblogs.com/sea-stream/p/14057617.html