Question
Problem 1 Threads and Concurrency The following exercise shows what can happen when two threads (here Person1 and Person2) share the same object (here a
Problem 1 Threads and Concurrency The following exercise shows what can happen when two threads (here Person1 and Person2) share the same object (here a common bank account). For that, you are going to write a program that will include two classes: Account and JobPerson1AndPerson2. The Account class is very simple. It includes: A private attribute balance initialized to 100 representing the current account balance. A withdraw method allowing to withdraw a certain amount from the account. Here it is in detail: class Account { private int balance = 100; public int getBalance () { return balance; } public void withdraw(int amount){ balance = balance - amount; } }// end class Account The JobPerson1AndPerson2 class implements Runnable and represents the behavior that Person1 and Person2 both have. Their behavior is quite particular since Person1 and Person2 fall asleep very often and in particular while they are withdrawing. This class contains: An attribute of type Account representing the bank account of Person1 and Person2. A method makeWithdrawal(int amount) allowing Person1 or Person2 to make a withdrawal from their bank account. The behavior of this method is as follows: the person wishing to make the withdrawal checks the balance, then falls asleep 500 ms, then on waking (after 500 ms) makes the withdrawal. The name of the person making the withdrawal must be reported. A run () method describing the behavior of Person1 and Person2. This involves making a loop (for example 10) withdrawals of 10 euros. Finally, the main method creates two threads (Person1 and Person2) with the same Runnable (here of type JobPerson1AndPerson2), names them Person1 and Person2, then starts them. Write the JobPerson1AndPerson2 java class.
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