zoukankan      html  css  js  c++  java
  • 多线程同步 锁的概念

    import time
    import threading
    
    num = 0  #定义一个全局变量
    tolock = threading.Lock()  # 创建一把锁
    class Mythread(threading.Thread):  #继承threading模块的Thread方法
        def run(self):  #对父类重写
            global num    #声名修改全局变量
            aa=tolock.acquire()  # 进行上锁
            print('线程(%s)的锁状态为%d'%(self.name,aa))
            if aa:  #判断上锁是否成功
                num=num+1   #全局变量+1
                time.sleep(1)
                msg = self.name+'set num to '+str(num)
                print(msg)
                tolock.release()  #释放锁
    def myt():
        for i in range(5):
            t=Mythread()    #创建实例
            t.start()   # 开启线程
    if __name__ == '__main__':
        myt()    #调用函数
    
  • 相关阅读:
    flash player over linux
    chmod 命令
    A*算法
    adb找不到设备
    ubuntu14.04安装wine以及国际版QQ
    linux man
    X-window
    linux file system
    linux command
    directUI
  • 原文地址:https://www.cnblogs.com/pp8080/p/12492076.html
Copyright © 2011-2022 走看看