python多线程同步

python多线程同步



作者:vpoet

日期:大约在夏季


import threading
import time
mylock = threading.RLock()  
num=0  
   
class myThread(threading.Thread):  
    def __init__(self, name):  
        threading.Thread.__init__(self)  
        self.t_name = name  
          
    def run(self):  
        global num  
        while True:  
            mylock.acquire()  
            print '\nThread(%s) locked, Number: %d'%(self.t_name, num)  
            if num>=10:  
                mylock.release()  
                print 'num >=10 thread over!'
                break  
            num+=1  
            print '\nThread(%s) released, Number: %d'%(self.t_name, num)
            time.sleep(1)
            mylock.release()  
              
def test():  
    thread1 = myThread('A')  
    thread2 = myThread('B')  
    thread1.start()  
    thread2.start()  
   
if __name__== '__main__':  
    test()  


运行结果:



你可能感兴趣的:(线程,python)