首页 > 编程语言 > 详细

多线程下的单例模式

时间:2019-01-04 11:19:36      阅读:124      评论:0      收藏:0      [点我收藏+]
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()

 

多线程下的单例模式

原文:https://www.cnblogs.com/zxmbky/p/10218480.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!