多任务-python实现-继承Thread类,单独编写一个类(2.1.2)

@

1.thread类

threding代码实现

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        for i in range(3):
            time.sleep(1)
            msg ="i am "+ self.name + "@" +str(i)
            print(msg)


if __name__ == '__main__':
        t = MyThread()
        t.start()

注意

  • 继承threding.Thread类
  • 必须有一个run方法,主要的逻辑就放在run方法中的
  • 在主线程中实例一个你编写的的thread类
  • 调用实例的start方法,会自动运行run方法中的逻辑
posted @ 2019-08-07 18:42  BothSavage  阅读(312)  评论(0编辑  收藏  举报