Question: Write a program that demonstrates thread cooperation. Suppose that you create and launch two threads, one deposits to an account, and the other withdraws from
Write a program that demonstrates thread cooperation. Suppose that you create and launch two threads, one deposits to an account, and the other withdraws from the same account. The second thread must wait to see if the amount to be withdrawn is more than the current balance in the account. Whenever new fund is deposited to the account, the first thread notifies the second thread to resume. If the amount is still not enough for a withdrawal, the second thread must continue to wait for more funds in the account. Assume the initial balance is and the amount to deposit and to withdraw is randomly generated.
An example of the program is shown below:
import threading
import random
import time
class Account:
def initself:
self.balance
self.condition threading.Condition
def depositself amount:
with self.condition:
self.balance amount
printfThread : Deposit amount
printfBalance: selfbalance
self.condition.notify
def withdrawself amount:
with self.condition:
while self.balance amount:
printfThread : Waiting for deposit"
self.condition.wait
self.balance amount
printfThread : Withdraw amount
printfBalance: selfbalance
def depositoraccount:
while True:
amount random.randint
account.depositamount
time.sleeprandomrandint
def withdraweraccount:
while True:
amount random.randint
account.withdrawamount
time.sleeprandomrandint
account Account
t threading.Threadtargetdepositor, argsaccount
t threading.Threadtargetwithdrawer, argsaccount
tstart
tstart
OUTPUT: No "public class" found to execute
How do I fix this? please help.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
