线程
#python多线程共享数据
import threading
import time
name_dict = {
"蔡徐坤":2000,
"鹿晗":2100,
"金星":2500
}
#申请一把锁
thlock = threading.Lock()
#支付
def zhifu(name,b):
#锁门
thlock.acquire()
a = name_dict[name]
#查询账户余额
print(f"当前账户余额:{a}")
time.sleep(3)
#假设我们进行了消费
name_dict[name] = a - b
#开锁
thlock.release()
#余额宝生成利息
def yuebao(name,b):
thlock.acquire()
a = name_dict[name]
# 查询账户余额
print(f"当前账户余额:{a}")
time.sleep(3)
# 假设我们进行了消费
name_dict[name] = a + b
#开锁
thlock.release()
th1 = threading.Thread(target=zhifu,args=("蔡徐坤",100))
th2 = threading.Thread(target=yuebao,args=("蔡徐坤",200))
#启动线程
th1.start()
th2.start()
#设置等待
th1.join()
th2.join()
print(name_dict["蔡徐坤"])
博客围绕Python多线程展开,虽未详细阐述内容,但明确提及线程相关,在信息技术领域,Python多线程是重要的编程概念,可用于提高程序执行效率等。
2919

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



