import threading lock = threading.Lock() current_number = 1 # 初始值为1,从奇数开始打印 def print_odd(): global current_number while current_number <= 10: lock.acquire() if current_number % 2 == 1: print("Odd:", current_number) current_number += 1 lock.release() def print_even(): global current_number while current_number <= 10: lock.acquire() if current_number % 2 == 0: print("Even:", current_number) current_number += 1 lock.release() thread_odd = threading.Thread(target=print_odd) thread_even = threading.Thread(target=print_even) thread_odd.start() thread_even.start() thread_odd.join() thread_even.join() print("Done")