python 单例模式

装饰器单例模式

 

 

 

 

 

import threading
import time
class  SingLeton(object):
    _instance = None
    lock = threading.RLock()
    def __new__(cls):
        if cls._instance:
            return cls._instance
        with cls.lock:
            if not cls._instance:
                time.sleep(1)
                cls._instance = super(SingLeton,cls).__new__(cls)
            return cls._instance

def fun():
    obj=SingLeton()
    print(obj)

for i in range(10):
    t = threading.Thread(target=fun)
    t.start()

 

posted @ 2020-08-12 10:23  第十一个程序员  阅读(74)  评论(0编辑  收藏  举报