zoukankan      html  css  js  c++  java
  • threading的join方法

    without join:
    +---+---+------------------ main-thread
    | |
    | +........... child-thread(short)
    +.................................. child-thread(long)

    with join
    +---+---+------------------***********+### main-thread
    | | |
    | +...........join() | child-thread(short)
    +......................join()...... child-thread(long)

    with join and daemon thread
    +-+--+---+------------------***********+### parent-thread
    | | | |
    | | +...........join() | child-thread(short)
    | +......................join()...... child-thread(long)
    +,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, child-thread(long + daemonized)

    '-' main-thread/parent-thread/main-program execution
    '.' child-thread execution
    '#' optional parent-thread execution after join()-blocked parent-thread could
    continue
    '*' main-thread 'sleeping' in join-method, waiting for child-thread to finish
    ',' daemonized thread - 'ignores' lifetime of other threads;
    terminates when main-programs exits; is normally meant for
    join-independent tasks

    
    #没有join
    
    # import threading
    # #定义线程要调用的方法,*add可接收多个以非关键字方式传入的参数
    # def action(*add):
    #     # print(*add)
    #     for arc in add:
    #         #调用 getName() 方法获取当前执行该程序的线程名
    #         print(threading.current_thread().getName() +" "+ arc)
    # #定义为线程方法传入的参数
    # my_tuple = ("http://c.biancheng.net/python/",
    #             "http://c.biancheng.net/shell/",
    #             "http://c.biancheng.net/java/")
    # #创建线程
    # thread = threading.Thread(target = action,args =my_tuple)
    # #启动线程
    # thread.start()
    # #主线程执行如下语句
    # for i in range(5):
    #     print(threading.current_thread().getName())
    
    
    
    #有join
    
    import threading
    #定义线程要调用的方法,*add可接收多个以非关键字方式传入的参数
    def action(add):
        for arc in add:
            #调用 getName() 方法获取当前执行该程序的线程名
            print(threading.current_thread().getName() +" "+ arc)
    #定义为线程方法传入的参数
    my_tuple = ("http://c.biancheng.net/python/",
                "http://c.biancheng.net/shell/",
                "http://c.biancheng.net/java/")
    #创建线程
    thread = threading.Thread(target = action,args =my_tuple)
    #启动线程
    thread.start()
    #指定 thread 线程优先执行完毕
    thread.join()
    #主线程执行如下语句
    for i in range(5):
        print(threading.current_thread().getName())
    
  • 相关阅读:
    [BZOJ 3282] Tree 【LCT】
    [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
    [BZOJ 1036] [ZJOI2008] 树的统计Count 【Link Cut Tree】
    [HDOJ
    Excel+DDT数据驱动实例
    jenkins+SVN配置
    [转]loadrunner:系统的平均并发用户数和并发数峰值如何估算
    loadrunner:Auto Correlate自动定位瓶颈
    loadrunner:判断是否服务器连接池瓶颈
    利用page_source抓取网页中的URL,进行链接测试
  • 原文地址:https://www.cnblogs.com/amize/p/14261839.html
Copyright © 2011-2022 走看看