import threading def sing(): print('进入sing -----------------') for i in range(3): print('进入sing循环 -----------------') l1.acquire() print('sing上锁 -----------------') print('唱歌。。。') try: print('dance解锁 -----------------') l2.release() print('dance解锁成功 -----------------') except Exception: print('sing中dance解锁失败,重新开始循环----------------') pass def dance(): print('进入dance -----------------') for i in range(3): print('进入dance循环 -----------------') l2.acquire() print('dance上锁 -----------------') print('跳舞。。。') try: print('sing解锁 -----------------') l1.release() print('sing解锁成功 -----------------') except Exception: print('dance中sing解锁失败,重新开始循环----------------') pass if __name__ == '__main__': # sing() # dance() l1 = threading.Lock() print(l1) l2 = threading.Lock() print(l2) t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) print('线程创建完毕--------') t1.start() print('线程1开始执行------') t2.start() print('线程2开始执行------') t1.join() print('线程1守护主线程-----') t2.join() print('线程2守护主线程-----')