# -*- encoding:utf-8 -*- ''' Created on Sep 18, 2012 @author: root write by wye in cloudiya ''' import threading import time data = 0 lock = threading.Lock() def func(): global data print "%s acquire lock...." % threading.currentThread().getName() #è°ç¨acquire([timeout])æ¶,线ç¨å°ä¸ç´é»å¡,ç´å°è·å¾é宿è ç´å°timeoutç§å(timeoutåæ°å¯é) if lock.acquire(): print "%s get the lock." % threading.currentThread().getName() data += 1 print data time.sleep(4) print "%s release lock ..." % threading.currentThread().getName() #è°ç¨release()å°éæ¾é lock.release() t1 = threading.Thread(target=func) t2 = threading.Thread(target=func) t3 = threading.Thread(target=func) t1.start() t2.start() t3.start() print data