看了好多文章都是java实现的,特此写一篇python的。
这个问题的两种解决方案:
1.最简单粗暴的就是在系统中先生成一个单例,就不存在线程安全问题了
2.用双重同步锁去实现,一把锁加外面,一把锁加里面:
class Singleton(object):
__instance = None
def __new__(cls, age, name):
# 加锁
if not cls.__instance:
#加锁
cls.__instance = object.__new__(cls)
return cls.__instance