Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%

In Python code:

Modify this threading example to use, exclusively, multiprocessing, instead of threading.

import threading
import time

class BankAccount():
def __init__(self, name, balance):
self.name = name
self.balance = balance

def __str__(self):
return self.name

# These accounts are our shared resources
account1 = BankAccount("account1", 100)
account2 = BankAccount("account2", 0)

class BankTransferThread(threading.Thread):
def __init__(self, sender, receiver, amount):
threading.Thread.__init__(self)
self.sender = sender
self.receiver = receiver
self.amount = amount

def run(self):
sender_initial_balance = self.sender.balance
sender_initial_balance -= self.amount
# Inserting delay to allow switch between threads
time.sleep(0.001)
self.sender.balance = sender_initial_balance

receiver_initial_balance = self.receiver.balance
receiver_initial_balance += self.amount
# Inserting delay to allow switch between threads
time.sleep(0.001)
self.receiver.balance = receiver_initial_balance

if __name__ == "__main__":

threads = []

for i in range(100):
threads.append(BankTransferThread(account1, account2, 1))

for thread in threads:
thread.start()

for thread in threads:
thread.join()

print(account1.balance)
print(account2.balance)

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education