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

python每日一题:锁知识点

时间:2019-01-02 21:40:59      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:target   bsp   执行顺序   顺序   import   pytho   show   reading   end   

import time
import threading

def show1():
    for i in range(1, 52, 2):
        lock_show2.acquire()
        print(i, end=‘‘)
        print(i+1, end=‘‘)
        time.sleep(0.2)
        lock_show1.release()


def show2():
    for i in range(26):
        lock_show1.acquire()
        print(chr(i + ord(A)))
        time.sleep(0.2)
        lock_show2.release()


lock_show1 = threading.Lock()
lock_show2 = threading.Lock()

show1_thread = threading.Thread(target=show1)
show2_thread = threading.Thread(target=show2)

lock_show1.acquire()  # 因为线程执行顺序是无序的,保证show1()先执行

show1_thread.start()
show2_thread.start()

 

python每日一题:锁知识点

标签:target   bsp   执行顺序   顺序   import   pytho   show   reading   end   

原文地址:https://www.cnblogs.com/xuehaiwuya0000/p/10211299.html

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