python 学习笔记03---线程调用的两种方式

1.直接启动线程模式

import threading
import time
 
def hello(num): 
 
    print("running on number:%s" %num)
 
    time.sleep(3)
 
if __name__ == '__main__':
 
    t1 = threading.Thread(target=hello,args=(1,)) 
    t2 = threading.Thread(target=hello,args=(2,)) 
 
    t1.start() 
    t2.start()
 
    print(t1.getName()) 

    print(t2.getName())


2、继承式调用

import threading
import time
 
 
class MyThread(threading.Thread):
    def __init__(self,num):
        threading.Thread.__init__(self)
        self.num = num
 
    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()


猜你喜欢

转载自blog.csdn.net/maibm/article/details/80922749
今日推荐