py07_03:互斥锁解决资源竞争问题

 

 

 

 

import threading
import time

init_total = 0
lock = threading.Lock()

def test_01():
    global init_total
    lock.acquire()
    for i in range(100000):
        init_total += 1
    lock.release()


def test_02():
    global init_total
    lock.acquire()
    for i in range(100000):
        init_total += 1
    lock.release()


def main():
    t_01 = threading.Thread(target=test_01)
    t_01.start()

    t_02 = threading.Thread(target=test_02)
    t_02.start()
    time.sleep(1)
    print(init_total)  # 多线程


if __name__ == '__main__':
    main()
上图代码

 

posted on 2020-03-26 09:31  yeyu1314  阅读(109)  评论(0编辑  收藏  举报