线程锁lock

from threading import Lock
lock = Lock() #创建锁对象
lock.acquire()#上锁,如果lock已经上锁在调用会阻塞
lock.release()#解锁
with lock:#shangs
    """"
    with代码块结束自动解锁
    """
from threading import Thread
from threading import Lock
lock = Lock()
a=b=0
def value():
    while True:
        lock.acquire()#上锁
        if a != b:
            print('a = %d,b = %d'%(a,b))
        lock.release()#解锁
t = Thread(target=value)
t.start()
while True:
    with lock:
        a+=1
        b+=1
t.join()


你可能感兴趣的:(java,服务器,linux)