美文网首页
多线程理解(不)可变对象的共享变量

多线程理解(不)可变对象的共享变量

作者: 蕴重Liu | 来源:发表于2019-07-16 17:59 被阅读0次
if __name__ == '__main__':
    # file = ''
    # read_excel(file.read())

    import threading
    balance = 1
    ccbalance = 1
    bbalance = {'count':0 }
    # 操作银行账户中的余额
    def run_thread(balance):
        for i in range(10000000):
            balance += 1
            ccbalance = ccbalance + 1
            bbalance['count'] += 1

    t1 = threading.Thread(target=run_thread, args=(balance, ))
    t2 = threading.Thread(target=run_thread, args=(balance, ))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print(balance)
   print(ccbalance)
    print(bbalance['count'])

输出:
1
1
15934810

相关文章

网友评论

      本文标题:多线程理解(不)可变对象的共享变量

      本文链接:https://www.haomeiwen.com/subject/bpmtlctx.html