#coding=gbk
import threading, time, random
count = 0
class Counter(threading.Thread):
def __init__(self, lock, threadName):
'''@summary: 初始化对象。
@param lock: 琐对象。
@param threadName: 线程名称。
'''
super(Counter, self).__init__(name = threadName)
#注意:一定要显式的调用父类的初始
化函数。
self.lock = lock
def run(self):
'''@summary: 重写父类run方法,在线程启动后执行该方法内的代码。
'''
global count
self.lock.acquire()
for i in xrange(10000):
count = count + 1
self.lock.release()
lock = threading.Lock()
for i in range(5):
Counter(lock, "thread-" + str(i)).start()
time.sleep(2)
#确保线程都执行完毕
print count
import threading, time, random
count = 0
lock = threading.Lock()
def doAdd():
'''@summary: 将全局变量count 逐一的增加10000。
'''
global count, lock
lock.acquire()
for i in xrange(10000):
count = count + 1
lock.release()
for i in range(5):
threading.Thread(target = doAdd, args = (), name = 'thread-' + str(i)).start()
time.sleep(2)
#确保线程都执行完毕
print count
def __init__(self, group=None, target=None, name=None, args=(), kwargs={})
Thread.getName() Thread.setName() Thread.name
Thread.ident
Thread.is_alive() Thread.isAlive()
Thread.join([timeout])
import threading, time
def doWaiting():
print 'start waiting:', time.strftime('%H:%M:%S')
time.sleep(3)
print 'stop waiting', time.strftime('%H:%M:%S')
thread1 = threading.Thread(target = doWaiting)
thread1.start()
time.sleep(1)
#确保线程thread1已经启动
print 'start join'
thread1.join()
#将一直堵塞,直到thread1运行结束。
print 'end join'
import threading lock = threading.Lock() #Lock对象 lock.acquire() lock.acquire() #产生了死琐。 lock.release() lock.release() import threading rLock = threading.RLock() #RLock对象 rLock.acquire() rLock.acquire() #在同一线程内,程序不会堵塞。 rLock.release() rLock.release()
Condition.notify_all() Condition.notifyAll()
#---- Condition
#---- 捉迷藏的游戏
import threading, time
class Hider(threading.Thread):
def __init__(self, cond, name):
super(Hider, self).__init__()
self.cond = cond
self.name = name
def run(self):
time.sleep(1)
#确保先运行Seeker中的方法
self.cond.acquire()
#b
print self.name + ': 我已经把眼睛蒙上了'
self.cond.notify()
self.cond.wait()
#c
#f
print self.name + ': 我找到你了 ~_~'
self.cond.notify()
self.cond.release()
#g
print self.name + ': 我赢了'
#h
class Seeker(threading.Thread):
def __init__(self, cond, name):
super(Seeker, self).__init__()
self.cond = cond
self.name = name
def run(self):
self.cond.acquire()
self.cond.wait()
#a #释放对琐的占用,同时线程挂起在这里,直到被notify并重新占
有琐。
#d
print self.name + ': 我已经藏好了,你快来找我吧'
self.cond.notify()
self.cond.wait()
#e
#h
self.cond.release()
print self.name + ': 被你找到了,哎~~~'
cond = threading.Condition()
seeker = Seeker(cond, 'seeker')
hider = Hider(cond, 'hider')
seeker.start()
hider.start()
#---- Event
#---- 捉迷藏的游戏
import threading, time
class Hider(threading.Thread):
def __init__(self, cond, name):
super(Hider, self).__init__()
self.cond = cond
self.name = name
def run(self):
time.sleep(1)
#确保先运行Seeker中的方法
print self.name + ': 我已经把眼睛蒙上了'
self.cond.set()
time.sleep(1)
self.cond.wait()
print self.name + ': 我找到你了 ~_~'
self.cond.set()
print self.name + ': 我赢了'
class Seeker(threading.Thread):
def __init__(self, cond, name):
super(Seeker, self).__init__()
self.cond = cond
self.name = name
def run(self):
self.cond.wait()
print self.name + ': 我已经藏好了,你快来找我吧'
self.cond.set()
time.sleep(1)
self.cond.wait()
print self.name + ': 被你找到了,哎~~~'
cond = threading.Event()
seeker = Seeker(cond, 'seeker')
hider = Hider(cond, 'hider')
seeker.start()
hider.start()
def hello(): print "hello, world" t = Timer(3, hello) t.start() # 3秒钟之后执行hello函数。
threading.active_count() threading.activeCount()
threading.current_thread() threading.currentThread()
threading.enumerate()
threading.setprofile(func)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有