-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession22(a).py
More file actions
50 lines (32 loc) · 1.04 KB
/
session22(a).py
File metadata and controls
50 lines (32 loc) · 1.04 KB
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
38
39
40
41
42
43
44
# Asynchronous -> Parallel(May Lead to mixed output)
# Synchronous -> When there are multiple threads and they are accessing the same shared object, then we need
# synchronisation
import threading
import time
# Lock Object
lock = threading.Lock()
class Printer:
def printDocuments(self, docName, times):
lock.acquire()
for i in range(1, times+1):
print(">> Printing {} Copy#{}".format(docName, i))
time.sleep(1)
lock.release()
class Desktop(threading.Thread):
def attachPrinter(self, printer):
self.printer = printer
def run(self):
self.printer.printDocuments("LearningPython.pdf", 10)
class Laptop(threading.Thread):
def attachPrinter(self, printer):
self.printer = printer
def run(self):
self.printer.printDocuments("LearningJava.pdf", 10)
printer = Printer()
# printer.printDocuments("LearningPython.pdf", 10)
desktop = Desktop()
desktop.attachPrinter(printer)
desktop.start()
laptop = Laptop()
laptop.attachPrinter(printer)
laptop.start()