zoukankan      html  css  js  c++  java
  • Python中threading模块的join函数

    Join的作用是阻塞进程直到线程执行完毕。通用的做法是我们启动一批线程,最后join这些线程结束,例如:

     1 for i in range(10):
     2 
     3     t = ThreadTest(i)
     4 
     5     thread_arr.append(t)
     6 
     7 for i in range(10):
     8 
     9     thread_arr[i].start()
    10 
    11 for i in range(10):
    12 
    13     thread_arr[i].join()

    此处join的原理就是依次检验线程池中的线程是否结束,没有结束就阻塞直到线程结束,如果结束则跳转执行下一个线程的join函数。

    而py的join函数还有一个特殊的功能就是可以设置超时,如下:

    Thread.join([timeout])

    Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

     

    也就是通过传给join一个参数来设置超时,也就是超过指定时间join就不在阻塞进程。而在实际应用测试的时候发现并不是所有的线程在超时时间内都结束的,而是顺序执行检验是否在time_out时间内超时,例如,超时时间设置成2s,前面一个线程在没有完成的情况下,后面线程执行join会从上一个线程结束时间起再设置2s的超时。

  • 相关阅读:
    「SDOI2009」Bill的挑战
    「HAOI2011」Problem c
    HDU3530【STL/单调队列/RMQ】
    HDU2874【倍增、ST】
    POJ2955【区间DP】
    SPOJ375 Query on a tree 【倍增,在线】
    训练[2]-DFS
    Fighting For 2017 Season Contest 1
    POJ2796/DP/单调栈
    New Year Tree 【DFS序+线段树区间查询修改+二进制保存状态】
  • 原文地址:https://www.cnblogs.com/nju2014/p/4613019.html
Copyright © 2011-2022 走看看