Last active
August 22, 2016 09:28
-
-
Save TimonPeng/a652123a2e6cdb4e5749eea6473a244f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding=utf-8 | |
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() | |
cond.wait() | |
cond.release() | |
if __name__ == '__main__': | |
try: | |
for i in range(10000): | |
Worker().start() # start a worker | |
except BaseException, e: | |
print "error: ", type(e), e | |
time.sleep(5) | |
print "max thread count : ", i | |
finally: | |
cond.acquire() | |
cond.notifyAll() | |
cond.release() | |
time.sleep(3) | |
print threading.currentThread().getName()," quit" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment