创建线程的两种方式

创建线程的两种方式

from threading import Thread
import time
# def run(name):
#     print('%s is running'%name)
#     time.sleep(3)
#     print('%s is over'%name)
#
#
# if __name__ == '__main__':
#     p = Thread(target=run, args=('jason',))
#     p.start()


'''
使用继承类来创建进程或者线程时,他会自动执行类中的run方法
'''
class MyThread(Thread):
    def __init__(self,name):
        super().__init__()
        self.name = name

    def run(self):
        print('%s is running' % self.name)
        time.sleep(3)
        print('%s is over' % self.name)


if __name__ == '__main__':
    p = MyThread('jason')
    p.start()

 

posted @ 2019-08-12 22:29  Nmdlao  阅读(147)  评论(0编辑  收藏  举报