对于全局变量,在多线程中要格外小心,否则容易造成数据错乱的情况发生
#coding=utf-8import threadingimport timeclass MyThread(threading.Thread):# 重写 构造方法def __init__(self,num,sleepTime):threading.Thread.__init__(self)self.num = numself.sleepTime = sleepTimedef run(self):self.num += 1time.sleep(self.sleepTime)print('线程(%s),num=%d'%(self.name, self.num))if __name__ == '__main__':mutex = threading.Lock()t1 = MyThread(100,5)t1.start()t2 = MyThread(200,1)t2.start()
import threadingfrom time import sleepdef test(sleepTime):num=1sleep(sleepTime)num+=1print('---(%s)--num=%d'%(threading.current_thread(), num))t1 = threading.Thread(target = test,args=(5,))t2 = threading.Thread(target = test,args=(1,))t1.start()t2.start()
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
同时也捎带提醒一句,如果你发现你 hook 系统的方法不起作用的时候,或许可以检查一下你项目里引入的第三方框架里是否也 hook 了和你一样的" alt="从使用 KVO 监听 readonly 属性说起">
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态