实现代码如下:

#多线程
import threading
import time
class test:
    def test1(self,a):
        for i in range(3):
            print(a)
            time.sleep(2)
    def test2(self,b):
        for i in range(5):
            print(b)
            time.sleep(2)
if __name__ == '__main__':
    t1=threading.Thread(target=test().test1,args=(1,))
    t2=threading.Thread(target=test().test2, args=(2,))
    #守护线程,主进程结束了但该守护线程没有运行完,守护进程就会被强制结束
    t2.setDaemon(True)
    #开启线程
    t1.start()
    t2.start()
    #阻塞线程,等待t1进程结束,再执行下面的代码
    t1.join()
    print('3')
posted on 2020-01-11 21:10  badbadboy  阅读(153)  评论(0编辑  收藏  举报