多线程下的单例模式
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()