完整的单例模式

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()
posted @ 2019-09-18 20:08  徐-  阅读(240)  评论(0编辑  收藏  举报