Description: In this programming assignment 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! ). In this case the deposits and withdrawals will be made by synchronized threads. Synchronization is required for two reasons (1) mutual exclusion (updates cannot be lost) and (2) 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 threads (cooperating synchronized threads). (In other words, this problem is similar to the producer/consumer problem where there is more than one producer and more than one consumer process active simultaneously.) If a withdrawal thread attempts to withdraw an amount greater than the current balance in the account then it must block itself and wait until a deposit has occurred before it can try again. As we covered in the lecture notes, this will require that the deposit threads signal all waiting withdrawal threads whenever a deposit is completed.

  • To keep things relatively simple as well as to see immediate results from a series of transactions (deposits and withdrawals) assume that deposits are made in amounts ranging from $1 to $200 (even dollars only) and withdrawals are made in amounts ranging from $1 to $50 (again, even dollars only).
  • You should have three deposit threads and five withdrawal threads executing simultaneously.
  • Once a deposit thread has executed, put it to sleep for few milliseconds or so (depends a little bit on the speed of your system as to how long you will want to sleep the depositor threads - basically we want to ensure a lot more withdrawals than deposits) to allow other threads to execute. This is the only situation in which a deposit thread will block.
  • For withdrawal threads, things will be a bit different depending on whether you are working on a single or multi-core processor.
    • For single core processors, once a withdrawal thread has executed, have it yield to another thread. Since the thread is giving up the processor voluntarily, it will be unlikely to run again (attempt a second withdrawal in a row), before another thread runs. Note however, that it does not prevent it from running again, if all other withdrawal threads are blocked and all depositors are sleeping, it will run again.
    • For multi-core processors, once a withdrawal thread 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 multi-core environment.
    • What we dont want to happen is a single withdrawal thread gaining the CPU and then executing a long sequence of withdrawal operations. Recall though that withdrawal threads block if they attempt to withdraw more than the current balance in the account.
  • Assume all threads have the same priority.
  • The output from your program must look reasonably similar to the sample output shown below.
  • Do not put the threads into a counted loop for your simulation. In other words, the run() method should be an infinite loop.
  • Do not use the Java synchronized statement. I want you to handle the locking and signaling yourself. No monitors!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.