多线程下的单例模式
Posted zxmbky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程下的单例模式相关的知识,希望对你有一定的参考价值。
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()
以上是关于多线程下的单例模式的主要内容,如果未能解决你的问题,请参考以下文章