# 创建一个Thread实例,传给它一个函数; import threading from time import sleep, ctime loops = [4,2] def loop(nloop, nsec): print "start loop", nloop, "at:",ctime() sleep(nsec) print "end loop", nloop, "at:",ctime() def main(): print "*****start*********", ctime() threads = [] nloops = range(len(loops)) for i in nloops: t = threading.Thread(target=loop, args=(i, loops[i])) threads.append(t) # start threads for i in nloops: threads[i].start() # wait for all threads to finish for i in nloops: threads[i].join() print "*******end*******", ctime() if __name__ == "__main__": main()
时间: 2024-10-07 01:32:11