Python之多线程


MyThread类是我自己实现的一个类,继承自threading模块中的Thread类,在子类中重写run方法,当进程调用start方法时候,子类的run方法会被调用!工作需要,现学现卖,献丑了!

'''
Created on May 28, 2013

@author: Berlin
'''
import threading

class MyThread(threading.Thread):
    def __init__(self, myId, count, mutex):
        self.myId = myId
        self.count = count
        self.mutex = mutex
        threading.Thread.__init__(self)
    
    def run(self):
        for i in range(self.count):
            with self.mutex:
                print('[%s] => %s' % (self.myId, i))

def Main():
    stdoutmutex = threading.Lock()
    threads = []
    for i in range(10):
        thread = MyThread(i, 100, stdoutmutex)
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()
    print('Main thread exiting.')
    
if __name__ == '__main__':
    Main()

谢谢阅读!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM