【Rollo的Python之路】Python 线程学习笔记(二)

1.0  面向对象的方法来创建线程:

import threading
import time

#面向对象很重要,面向对象很重要,面向对象很重要


class Mythread(threading.Thread):

    def __init__(self,num):
        threading.Thread.__init__(self)
        self.num = num  #self.num是个实例变量,存在t1/t2的对象里面

    #run是threading的内置方法
    def run(self):
        print("running on number:%s" % self.num)
        time.sleep(3)

if __name__ == '__main__':
    t1 = Mythread(1)  #实例化一个对像
    t2 = Mythread(2)  #实例化一个对像

    t1.start()
    t2.start()

猜你喜欢

转载自www.cnblogs.com/rollost/p/10920342.html