-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.py
More file actions
37 lines (27 loc) · 870 Bytes
/
Copy pathsingleton.py
File metadata and controls
37 lines (27 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# ----------------------------------------------------------------------------------------------------
# 单例模式
#
# 作者:xxrBear
# 创建时间:2025-06-19
# ----------------------------------------------------------------------------------------------------
import threading
class Singleton:
"""最简单的单例模式"""
_instance = None
def __new__(cls):
if cls is None:
cls._instance = super().__new__(cls)
return cls._instance
else:
return cls._instance
class MultiThreadSingleton:
"""多线程版本"""
_instance = None
_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
with cls._lock:
cls._instance = super().__new__(cls)
return cls._instance
else:
return cls._instance