多线程下的单例模式

import threading
import time

class Foo:
    _instance = None
    _lock = threading.RLOCK()
    
    def __new__(cls,*args,**kwargs):
        if cls._instance:
            return cls._instance
        with cls._lock:
            if not cls._instance = object.__new__(cls)
        return cls._instance
def task():
    obj=Foo()
    print(obj)
for i in range(10):
    t=threading.Thread(target=task)
    t.start()

 

posted @ 2019-01-04 10:35  显示名称已经被使用  阅读(139)  评论(0编辑  收藏  举报