多线程实现单例模式

 

 http://www.cnblogs.com/huchong/p/8244279.html

代码:

import threading


def synchronized(func):
    func.__lock__ = threading.Lock()

    def lock_func(*args, **kwargs):
        with func.__lock__:
            return func(*args, **kwargs)

    return lock_func


class Singleton(object):
    instance = None

    @synchronized
    def __new__(cls, *args, **kwargs):
        """
        :type kwargs: object
        """
        if cls.instance is None:
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self, num):
        self.a = num + 5

    def printf(self):
        print(self.a)


a = Singleton(3)
print(id(a))
b = Singleton(4)
print(id(b))
View Code

 

posted @ 2019-05-18 20:14  ChuckXue  阅读(166)  评论(0编辑  收藏  举报