import threading
import time
class PrintNum(threading.Thread):
def run(self):
for i in range(1, 52, 2):
lock_show2.acquire()
print(i, end="")
print(i + 1, end="")
time.sleep(0.2)
lock_show1.release()
class PrintWord(threading.Thread):
def run(self):
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()
thread_one = PrintNum()
thread_two = PrintWord()
# 因为线程执行是无序的,保证show1先执行
lock_show1.acquire()
thread_one.start()
thread_two.start()
Python:简单的多线程操作
最新推荐文章于 2024-07-20 17:12:48 发布