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())
    
  • 相关阅读:
    JavaScript 严格模式
    JavaScript 编码规范 之 循环语句
    分享21个基于jquery菜单导航的效果
    揭秘:淘宝搜索排名真正规则和技巧
    11款网站死链检测工具
    收集Windows 8 Metro UI 风格网站资源,觉得不错的顶啊!!
    29个非常流行的jQuery提示信息插件
    分享14个很酷的jQuery导航菜单插件
    jquery select 常用操作总结
    必须去收藏14个响应式布局的前端开发框架
  • 原文地址:https://www.cnblogs.com/amize/p/14261839.html
Copyright © 2011-2022 走看看