import threading
import time
class Test:
cache = {}
@classmethod
def get_value(self, key):
value = Test.cache.get(key, [])
return len(value)
@classmethod
def store_value(self, key, value):
if not Test.cache.has_key(key):
Test.cache[key] = range(value)
else:
Test.cache[key].extend(range(value))
return len(Test.cache[key])
@classmethod
def release_value(self, key):
if Test.cache.has_key(key):
Test.cache.pop(key)
return True
@classmethod
def print_cache(self):
print 'print_cache:'
for key in Test.cache:
print 'key: %d, value:%d' % (key, len(Test.cache[key]))
def worker(number, value):
key = number % 5
print 'threading: %d, store_value: %d' % (number, Test.store_value(key, value))
time.sleep(10)
print 'threading: %d, release_value: %s' % (number, Test.release_value(key))
if __name__ == '__main__':
thread_num = 10
thread_pool = []
for i in range(thread_num):
th = threading.Thread(target=worker,args=[i, 1000000])
thread_pool.append(th)
thread_pool[i].start()
for thread in thread_pool:
threading.Thread.join(thread)
Test.print_cache()
time.sleep(10)
thread_pool = []
for i in range(thread_num):
th = threading.Thread(target=worker,args=[i, 100000])
thread_pool.append(th)
thread_pool[i].start()
for thread in thread_pool:
threading.Thread.join(thread)
Test.print_cache()
time.sleep(10)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有