Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

=+What is your maiden name?

Answered: 1 week ago

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago