使用一个循环,不断的创建线程,直到出现异常,才通知它们。python真是个好东西。
#!/usr/bin/env python
#coding=gbk
import threading
import time, random, sys
class Counter:
def __init__(self):
self.lock = threading.Lock()
self.value = 0
def increment(self):
self.lock.acquire()
self.value = value = self.value + 1
self.lock.release()
return value
counter = Counter()
cond = threading.Condition()
class Worker(threading.Thread):
def run(self):
print self.getName(), "-- created."
cond.acquire()
#for i in range(10):
# pretend we're doing something that takes 10?00 ms
#value = counter.increment()
# time.sleep(random.randint(10, 100) / 1000.0)
cond.wait()
#print self.getName(), "-- task", "finished&