Question: Need some help on the following JAVA problem: Write a class called TellerMachine that simulates an ATM. It should contain two methods deposit() and withdraw().
Need some help on the following JAVA problem:
Write a class called TellerMachine that simulates an ATM. It should contain two methods deposit() and withdraw(). It also contains a private variable called accountBalance. The deposit() method allows a thread to deposit $500, and update the account balance. The withdraw() method allows a thread to remove $120, and update the account balance. Include the following lines in both methods to invoke a threads sleep method and print out the thread name and account balance as follows:
try {
Thread.sleep((int)(Math.random() * 1000) );
} catch(InterruptedException e) {
System.out.printf(Thread is interrupted);
}
System.out.println ( Threadname: + Thread.currentThread().getName() + Balance: +accountBalance );
Write a main method that creates two threads. One of the threads (called Deposit Thread) should execute the deposit() method. The other thread (called Withdraw Thread) should execute the withdraw() method. Start the threads and examine the output of your program. It should be similar to the one below:
Threadname:Deposit Thread Balance:500
Threadname:Withdrawal Thread Balance:380
Threadname:Deposit Thread Balance:880
Threadname:Withdrawal Thread Balance:760
Threadname:Deposit Thread Balance:1260
Threadname:Withdrawal Thread Balance:1140
You need to make sure that there is no race occurs between the deposit and withdraw threads.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
