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())
    
  • 相关阅读:
    OleDbCommand 的用法
    递归求阶乘
    C#重写窗体的方法
    HDU 5229 ZCC loves strings 博弈
    HDU 5228 ZCC loves straight flush 暴力
    POJ 1330 Nearest Common Ancestors LCA
    HDU 5234 Happy birthday 01背包
    HDU 5233 Gunner II 离散化
    fast-IO
    HDU 5265 pog loves szh II 二分
  • 原文地址:https://www.cnblogs.com/amize/p/14261839.html
Copyright © 2011-2022 走看看