首页 > 学院 > 开发设计 > 正文

thread模块

2019-11-06 07:48:09
字体:
来源:转载
供稿:网友

# -*- coding: UTF-8 -*-import threadimport timedef loop0():    PRint "start loop0 at :" , time.ctime()    time.sleep(4)    print "loop0 done at :", time.ctime()    def loop1():    print "start loop1 at:", time.ctime()    time.sleep(4)    print "loop1 done at:", time.ctime()    def main():    print "starting at :", time.ctime()    thread.start_new_thread(loop0, ())    thread.start_new_thread(loop1, ())    time.sleep(6)    print "all done at: ", time.ctime()    if __name__ == "__main__":    main()

# -*- coding: UTF-8 -*-import threadimport timeloops = [4, 2]def loop(nloop, nesc, lock):    print "start loop", nloop, "at:", time.ctime()    time.sleep(nesc)    print "loop", nloop, "done at:", time.ctime()    lock.release()    def main():    print "starting at ", time.ctime()    locks = []    nloops = range(len(loops))        for i in nloops:        lock = thread.allocate_lock()        lock.acquire()        locks.append(lock)            for i in nloops:        thread.start_new_thread(loop, (i, loops[i], locks[i]))            for i in nloops:        while locks[i].locked():            pass                print "all done at:", time.ctime()    if __name__ == "__main__":    main()


上一篇:约瑟夫环问题

下一篇:[HDU1166]敌兵布阵

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表