self_example/TensorFlow_eaxmple/threading_try/Lock类.py

25 lines
530 B
Python

# coding =utf8
import threading, time
count = 0
class MyThread(threading.Thread):
def __init__(self, lock, threadName):
super(MyThread, self).__init__(name=threadName)
self.lock = lock
def run(self):
global count
self.lock.acquire()
for i in range(10):
count = count + 1
time.sleep(0.3)
print(self.getName(), count)
self.lock.release()
lock = threading.Lock()
for i in range(2):
MyThread(lock, "MythreadName:" + str(i)).start()