用通俗易懂的方式讲解python多线程
发布网友
发布时间:2022-04-27 09:16
我来回答
共1个回答
热心网友
时间:2022-04-07 12:32
import threading
import time
class Procer(threading.Thread):
def run(self):
global count
while True:
if con.acquire():
if count > 1000:
con.wait()
else:
count = count+100
msg = self.name+' proce 100, count=' + str(count)
print msg
con.notify()
con.release()
time.sleep(1)
class Consumer(threading.Thread):
def run(self):
global count
while True:
if con.acquire():
if count < 100:
con.wait()
else:
count = count-3
msg = self.name+' consume 3, count='+str(count)
print msg
con.notify()
con.release()
time.sleep(1)
count = 500
con = threading.Condition()
def test():
for i in range(2):
p = Procer()
p.start()
for i in range(5):
c = Consumer()
c.start()
if __name__ == '__main__':
test()
http://www.cnblogs.com/holbrook/archive/2012/03/13/2394811.html