python多线程:join对执行顺序的影响

python多线程:join对执行顺序的影响,第1张

代码1

注意join的位置

import threading

import time

print('程序开始时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
start = time.time()


def thread_1():
    print("T1 start\n")
    for i in range(10):
        time.sleep(0.1)
    end = time.time()
    print('thread_1程序结束时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    print('thread_1程序用时:', end - start, 's')
    print("T1 finish\n")


def thread_2():
    print("T2 start\n")
    for i in range(10):
        time.sleep(0.1)
    end = time.time()
    print('thread_2程序结束时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    print('thread_2程序用时:', end - start, 's')
    print("T2 finish\n")


def main():
    thread1 = threading.Thread(target=thread_1)
    thread1.start()
    # thread1.join()  # join的作用: 后面的代码等thread1进程运行完后再运行
    thread2 = threading.Thread(target=thread_2)
    thread2.start()
    # thread2.join()
    print("all done\n")
    end = time.time()
    print('main程序结束时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    print('main程序用时:', end - start, 's')


if __name__ == '__main__':
    main()

无join的控制台输出

"C:\Program Files\Python310\python.exe" D:/workspace/Python_workspace/YunShiWei/th.py
程序开始时间: 2022-05-08 22:29:19
T1 start

T2 start

all done

main程序结束时间: 2022-05-08 22:29:19
main程序用时: 0.0019943714141845703 s
thread_2程序结束时间: 2022-05-08 22:29:20
thread_2程序用时: 1.1100966930389404 s
T2 finish

thread_1程序结束时间: 2022-05-08 22:29:20
thread_1程序用时: 1.1100966930389404 s
T1 finish


进程已结束,退出代码0

有join的控制台输出

"C:\Program Files\Python310\python.exe" D:/workspace/Python_workspace/YunShiWei/th.py
程序开始时间: 2022-05-08 22:29:37
T1 start

thread_1程序结束时间: 2022-05-08 22:29:38
thread_1程序用时: 1.1040937900543213 s
T1 finish

T2 start

thread_2程序结束时间: 2022-05-08 22:29:39
thread_2程序用时: 2.217207431793213 s
T2 finish

all done

main程序结束时间: 2022-05-08 22:29:39
main程序用时: 2.217207431793213 s

进程已结束,退出代码0

代码2

注意join的位置

import threading

import time

print('程序开始时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
start = time.time()


def thread_1():
    print("T1 start\n")
    for i in range(10):
        time.sleep(0.1)
    end = time.time()
    print('thread_1程序结束时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    print('thread_1程序用时:', end - start, 's')
    print("T1 finish\n")


def thread_2():
    print("T2 start\n")
    for i in range(10):
        time.sleep(0.1)
    end = time.time()
    print('thread_2程序结束时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    print('thread_2程序用时:', end - start, 's')
    print("T2 finish\n")


def main():
    thread1 = threading.Thread(target=thread_1)
    thread2 = threading.Thread(target=thread_2)
    thread1.start()
    thread2.start()
    thread1.join()  # join的作用: 后面的代码等thread1进程运行完后再运行
    thread2.join()
    print("all done\n")
    end = time.time()
    print('main程序结束时间:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    print('main程序用时:', end - start, 's')


if __name__ == '__main__':
    main()

控制台输出(有join)

"C:\Program Files\Python310\python.exe" D:/workspace/Python_workspace/YunShiWei/th.py
程序开始时间: 2022-05-08 22:42:57
T1 start

T2 start

thread_2程序结束时间:thread_1程序结束时间: 2022-05-08 22:42:58
thread_1程序用时:  1.1070094108581543 s
2022-05-08 22:42:58
thread_2程序用时: 1.1070094108581543 s
T2 finish

T1 finish

all done

main程序结束时间: 2022-05-08 22:42:58
main程序用时: 1.1070094108581543 s

进程已结束,退出代码0

大佬对join的讲解:【莫烦Python】Threading 学会多线程 Python_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1jW411Y7Wj?p=3

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/874759.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存