python # 多线程之join

本文所用程序来自https://www.jianshu.com/p/0e4ff7c856d3中的程序。

  • 例1:
import time
import threading

class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print('thread {}, @number: {}'.format(self.name, i))
            time.sleep(1)

def main():
    print("****************Start main threading****************")
    # 创建三个线程
    threads = [MyThread() for i in range(3)]
    # 启动三个线程
    for t in threads:
        t.start()

    print("****************End Main threading*****************")


if __name__ == '__main__':
    main()

运行结果:

****************Start main threading****************
thread Thread-1, @number: 0
thread Thread-2, @number: 0
thread Thread-3, @number: 0
****************End Main threading*****************
thread Thread-1, @number: 1
thread Thread-3, @number: 1
thread Thread-2, @number: 1
thread Thread-1, @number: 2
thread Thread-3, @number: 2
thread Thread-2, @number: 2
thread Thread-2, @number: 3
thread Thread-1, @number: 3
thread Thread-3, @number: 3
thread Thread-3, @number: 4
thread Thread-1, @number: 4
thread Thread-2, @number: 4

可以看出: 主线程结束后,新建的线程还在运行

  • 例2:
import time
import threading

class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print('thread {}, @number: {}'.format(self.name, i))
            time.sleep(1)

def main():
    print("****************Start main threading****************")
    # 创建三个线程
    threads = [MyThread() for i in range(3)]
    # 启动三个线程
    for t in threads:
        t.start()
        if t.isAlive():
            t.join()

    print("****************End Main threading*****************")


if __name__ == '__main__':
    main()

运行结果:

****************Start main threading****************
thread Thread-1, @number: 0
thread Thread-1, @number: 1
thread Thread-1, @number: 2
thread Thread-1, @number: 3
thread Thread-1, @number: 4
thread Thread-2, @number: 0
thread Thread-2, @number: 1
thread Thread-2, @number: 2
thread Thread-2, @number: 3
thread Thread-2, @number: 4
thread Thread-3, @number: 0
thread Thread-3, @number: 1
thread Thread-3, @number: 2
thread Thread-3, @number: 3
thread Thread-3, @number: 4
****************End Main threading*****************

线程依次运行,一个执行完之后,再执行下一个。这种方式比较慢。*

  • 例3:
import time
import threading

class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print('thread {}, @number: {}'.format(self.name, i))
            time.sleep(1)

def main():
    print("****************Start main threading****************")
    # 创建三个线程
    threads = [MyThread() for i in range(3)]
    # 启动三个线程
    for t in threads:
        t.start()

    for t in threads:
        if t.isAlive():
            t.join()

    print("****************End Main threading*****************")


if __name__ == '__main__':
    main()

运行结果:

****************Start main threading****************
thread Thread-1, @number: 0
thread Thread-2, @number: 0
thread Thread-3, @number: 0
thread Thread-2, @number: 1
thread Thread-1, @number: 1
thread Thread-3, @number: 1
thread Thread-1, @number: 2
thread Thread-2, @number: 2
thread Thread-3, @number: 2
thread Thread-3, @number: 3
thread Thread-1, @number: 3
thread Thread-2, @number: 3
thread Thread-2, @number: 4
thread Thread-1, @number: 4
thread Thread-3, @number: 4
****************End Main threading*****************

References:

https://www.jianshu.com/p/0e4ff7c856d3

你可能感兴趣的:(python # 多线程之join)