完整的单例模式
import time
import threading
class Singsing:
lock = threading.Lock()
instance = None
def __init__(self, name):
self.name = name
def __new__(cls, *args, **kwargs):
if cls.instance:
return cls.instance
with cls.lock:
if cls.instance:
return cls.instance
time.sleep(0.1)
cls.instance = object.__new__(cls)
return cls.instance
def task():
singer = Singsing('name')
print(singer)
for i in range(10):
t = threading.Thread(target=task)
t.start()