#线程锁的简单例子

import threading

import time

x=0

lock=threading.RLock()

class myThread(threading.Thread):

def run(self):

global x

lock.acquire()

for i in range(3):

x+=5

print(x)

lock.release()




def main():

thr=[]


for i in range(6):

thr.append(myThread())

for item in thr:

item.start()

if __name__=="__main__":

main()
输出:
15
30
45
60
75
90
#线程锁的简单例子
import threading
import time

class myThread(threading.Thread):
def run(self):
global x

for i in range(3):
x+=5
time.sleep(1)
print(x)



x=0
lock=threading.RLock()
def main():
thr=[]

for i in range(5):
thr.append(myThread())
for item in thr:
item.start()
if __name__=="__main__":
main()

输出:75
75
75
75
75