码迷,mamicode.com
首页 > 编程语言 > 详细

Python实现线程交替打印字符串

时间:2019-09-05 23:09:55      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:star   com   python实现   线程   tps   字符串   targe   bre   col   

 

import threading
con = threading.Condition()
word = u"12345上山打老虎"

def work():
    global word
    con.acquire()
    while True:
        print word[0]
        word = word[1:]
        if len(word) == 0:
            break
        con.notify()
        if len(word) == 1:
            break
        con.wait()
    con.release()

t1 = threading.Thread(target=work)
t2 = threading.Thread(target=work)
t1.start()
t2.start()
t1.join()
t2.join()

 

python条件变量知识参考:https://www.cnblogs.com/holbrook/archive/2012/03/13/2394811.html

Python实现线程交替打印字符串

标签:star   com   python实现   线程   tps   字符串   targe   bre   col   

原文地址:https://www.cnblogs.com/weswes/p/11470280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!