python多线程

import threading
import time


class Mythread(threading.Thread):
    def run(self):
        global num
        if mutex.acquire():
            num+=1
            print num
            mutex.release()

num=0
mutex=threading.Lock()
def test():
    for i in range(5):
        t=Mythread()
        t.start()

if __name__ == '__main__':  
    test()
    
'''
class mythread(threading.Thread):
    def __init__(self,num):
        threading.Thread.__init__(self)
        self.num=num
    def run(self):
        print self.num

for i in range(5):
    t=mythread(i)
    t.start()
#t=threading.Thread(target=run,args=(15,20))
#t.start()       
'''

  

posted @ 2014-03-27 11:59  huangxiaohen  阅读(144)  评论(0编辑  收藏  举报