"""
;线程锁示例
"""
import threading
import time
num = 100
lock = threading.Lock()
def func():
global num
"""
with 语句是 Python 中一种用于资源管理的语法结构。
通常用于确保在离开 with 块时资源(如文件、数据库连接、线程锁等)得到正确地分配和释放
"""
with lock:
tmp = num
time.sleep(0.001)
num = tmp - 1
threads = []
for i in range(100):
t = threading.Thread(target=func)
t.start()
threads.append(t)
for th in threads:
th.join()
print(f'number is: {num}')

博客涉及Python开发语言,但内容缺失,推测可能围绕Python的特性、应用场景等信息技术相关方面展开。Python是后端开发常用语言,有广泛用途。
156

被折叠的 条评论
为什么被折叠?



