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()
复制代码

谢谢阅读!

posted @   薛定谔の喵  阅读(680)  评论(8编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示