1. 程式人生 > >python每日一題:鎖知識點

python每日一題:鎖知識點

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()