git'n a system-desgin-primer

This commit is contained in:
Aditi
2024-10-23 10:51:05 +05:30
parent 3df19b2e4f
commit d0a17857cb
22 changed files with 725 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import threading
class Document:
def __init__(self, title):
self.title = title
self.content = ""
self.lock = threading.Lock()
def edit(self, new_content):
with self.lock:
self.content = new_content
print(f"Document '{self.title}' updated: {self.content}")
class User:
def __init__(self, user_id, name):
self.user_id = user_id
self.name = name
def edit_document(self, document, new_content):
print(f"{self.name} is editing the document '{document.title}'...")
document.edit(new_content)
# Example Usage
document = Document("Project Plan")
user1 = User(1, "Alice")
user2 = User(2, "Bob")
# Simulating concurrent edits
thread1 = threading.Thread(target=user1.edit_document, args=(document, "Draft by Alice."))
thread2 = threading.Thread(target=user2.edit_document, args=(document, "Draft by Bob."))
thread1.start()
thread2.start()
thread1.join()
thread2.join()

View File

@@ -0,0 +1,7 @@
1. Document: Represents a document being edited, with content locked for
safe concurrent editing.
2. User: Represents a user editing the document.
3. Each user can edit the document concurrently, with proper locking to prevent
data corruption.