Question
9.6 Write a program that has multiple threads that make deposits and withdrawals in a shared bank account program without using locks. Demonstrate how the
9.6 Write a program that has multiple threads that make deposits and withdrawals in a shared bank account program without using locks. Demonstrate how the bank account can become corrupted.
Assignment Requirements:
Exercise 9.6 a. Use a single shared integer buffer. b. The deposit amount is a random number 0 <= deposit <= 100 c. Use Main9Point6.java to test your classes. d. Program output is similar to Output9.10.txt.
Main9Point6.java
class Main9Point6 { // This starts everything
public static void main (String argv[]) {
UnsynchronizedBankAccount account = new
UnsynchronizedBankAccount(); // shared integer buffer
// start threads
new Thread (new Withdrawer(account, 1)).start();
new Thread (new Depositor(account, 1)).start();
new Thread (new Withdrawer(account, 2)).start();
new Thread (new Depositor(account, 2)).start();
}
}
Output9.10.txt
Withdrawer-2: 7
Withdrawer-1: 76
Depositor-2: 76
Depositor-1: 7
Withdrawer-2: 30
Depositor-2: 30
Withdrawer-1: 2
Depositor-1: 2
Withdrawer-2: 89
Depositor-2: 89
Depositor-1: 73
Withdrawer-1: 73
Withdrawer-1: 41
Withdrawer-2: 57
Depositor-1: 41
Depositor-2: 57
Withdrawer-2: 89
Depositor-2: 89
Withdrawer-1: 36
Depositor-1: 36
Depositor-2: 56
Depositor-1: 14
Withdrawer-1: 56
Withdrawer-2: 14
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