Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this project you will simulate the deposits and withdrawals made to a fictitious bank account ( I ll let you use my real bank
In this project you will simulate the deposits and withdrawals made to a
fictitious bank account Ill let you use my real bank account if you promise to make
only deposits! J In this case the deposits and withdrawals will be made by user
agents synchronized threads Synchronization is required for two reasons
mutual exclusion updates cannot be lost and because a withdrawal cannot occur
if the amount of the withdrawal request is greater than the current balance in the
account. This means that access to the account the shared object must be
synchronized. This application requires cooperation and communication amongst the
various agents cooperating synchronized threadsIn other words, this problem is
similar to the producerconsumer problem where there is more than one producer and
more than one consumer process active simultaneously. If a withdrawal agent
attempts to withdraw an amount greater than the current balance in the account then
it must block itself and wait until a depositing agent has added money to the account
before it can try again. As we covered in the lecture notes, this will require that the
depositing agents signal all waiting withdrawing agents whenever a deposit is
completed.
You should have five depositor agents threads and ten withdrawal agents
threads and two auditor agents threads simultaneously executing. Use a
FixedThreadPool and an Executor object to control the threads.
To keep things relatively simple, assume that deposits are made in amounts
ranging from $ to $whole dollars only and withdrawals are made in
amounts ranging from $ to $again whole dollars only Since we have
more withdrawal threads than depositor threads, the account balance should
constantly decrease over time. This will lead to withdrawal agents repeatedly
blocking for insufficient funds. Start the simulation with a balance of $ in the
account.
CNT Project Spring
Page
Once a depositor agent thread has deposited into the account, put it to sleep for
few milliseconds randomly generate this number dont use a constant sleep
time or so depends a little bit on the speed of your system as to how long you
will want to sleep the depositing threads basically we want to ensure a lot more
withdrawals than deposits to allow other agents to execute.
For withdrawal agents, things will be a bit different depending on whether you
are working on a single or multicore processor.
a For single core processors, once a withdrawal agent has withdrawn funds
from the account, have it yield the processor unit. Since the agent is giving
up the processor voluntarily, it will be unlikely to run again attempt a
second withdrawal in a row before another agent runs. Note however,
that it does not prevent it from running again, if all other withdrawal
agents are blocked and all depositors are sleeping, it will run again. So
occasional backtoback runs of withdrawal agents might occur see
below
b For multicore processors, once a withdrawal agent has executed, have it
sleep for some random period of time again a few milliseconds should
be fine Depending on which core a thread is executing, yielding the CPU
wont ensure that the same thread will not run again immediately. While,
sleeping the thread will also not ensure that it will not run two or more
times in succession, it is less likely to do so in the multicore environment.
c What we dont want to happen is a single withdrawal agent gaining the
CPU and then executing a long sequence of withdrawal operations. Recall
though, that withdrawal agents block if they attempt to withdraw more
than the current balance in the account.
d Similarly, we dont want depositor agents monopolizing the CPU either
and causing the balance in the account to grow continuously. This would
most likely occur when the withdrawal agents are sleeping too long in
comparison to the average sleep time of the depositing agents. See page
for an illustration of this.
Assume all depositor, withdrawal, and auditor agents have the same priority. Do
not give different priority to depositor, withdrawal, and auditor agents threads
The auditor agents will also have normal priority and will simply run less
frequently than the depositor and withdrawal threads ie the auditor agents will
sleep longer between runs than either depositor or withdrawal agents See
below.
Page
The output from your program must look reasonably similar to the sample output
shown below. The simulation output should show the action of each agent along
with the account balance produced by the agents transaction and the transaction
number.
Do not put the threads into a counted loop for your simulation. In other
words, the run method for all threads should be an infinite loop. Just stop
the simulation from your IDE after a few seconds.
Do not use the Java synchroni
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started