Upload New File

parent c1c0f8f6
import threading
import time
class UnsafeSingleton:
_instance = None
def __new__(cls):
if cls._instance is None:
time.sleep(0.01)
cls._instance = super(UnsafeSingleton, cls).__new__(cls)
return cls._instance
instances = []
def create_instance():
instance = UnsafeSingleton()
instances.append(instance)
threads = [threading.Thread(target=create_instance) for _ in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
unique_instances = set(instances)
print(f"Создано {len(unique_instances)} уникальных экземпляров Singleton.")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment