zoukankan      html  css  js  c++  java
  • threading的join功能

    you can go here to see the details.

    我的理解:使用join后,使主线程执行完后(挂起),子线程再执行完后,主线程才结束。否则主线程会直接结束导致子线程不会执行。

    import time
    import threading
    
    
    def testRun():
        print('-----start-----')     # 1. 执行
        time.sleep(2)    # 2. 阻塞(挂起,执行下一个线程:1和2步循环,直到所有线程都执行完 1,然后接着往下执行3)
        print(f"当前线程名字:{threading.current_thread().name}")  # 3  (3和4循环,直到所有线程都执行完3,然后接着往下执行5...)
        time.sleep(2)  # 4
        print('qeada')  # 5
    
    
    thread_list = []
    start = time.time()
    for i in range(5):
        t = threading.Thread(target=testRun)
        thread_list.append(t)
    for t in thread_list:
        t.setDaemon(True)
        t.start()
    for t in thread_list:  # 不使用join,会导致所有子线程遇到阻塞后,主线程并不会等待子线程阻塞结束而直接结束。
        t.join()  
    print("it's over.")
    print(f"总耗时:{time.time()-start}")
    
    >>>
    -----start-----
    -----start-----
    -----start-----
    -----start-----
    -----start-----
    当前线程名字:Thread-1
    当前线程名字:Thread-2
    当前线程名字:Thread-4
    当前线程名字:Thread-3
    当前线程名字:Thread-5
    qeada
    qeada
    qeada
    qeada
    qeada
    it's over.
    总耗时:4.006011962890625
  • 相关阅读:
    CSS 的 outline 属性
    CSS3 的 boxsizing 属性
    CSS 颜色的使用
    动态添加tab(ext)
    showModalDialog()重新加载问题
    DBUtility.SQLServerHelper”的类型初始值设定项引发异常
    终于有了思路
    js判断上传的图片格式
    session丢失 frame
    动态添加tab(ext中的treePanel)
  • 原文地址:https://www.cnblogs.com/tangpg/p/9590501.html
Copyright © 2011-2022 走看看