Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Objectives

This is an introductory assignment on Java synchronization. You will use Java Threads while learning more about concurrency and achieving atomicity using Java’s inbuilt mechanisms.

Source Code

There are four files provided with the assignment. You will need these files to work on the assignment.

The files are available in the zip file 1.zip.

File Checklist

Account.java

AccountManager.java

Depositor.java

Withdrawer.java

Background

In this assignment, you will deal with concurrent executions of multiple threads operating on a shared data structure (Accounts). You will be learning more about Java threads and synchronization mechanisms as you work on this assignment.

An array of accounts holds 10 accounts information (imagine an OS shared resource of the same type).

The account array has a valid state once it is initialized. You will employ 10 threads for deposit and 10 threads for withdrawal; each thread is bound to a specific account. There will be one depositor thread and one withdrawer thread that are bound to one

specific account. The depositor is responsible for depositing X amount to the account and the withdrawer is responsible for withdrawing the same X amount from the same account. As a result of running 20 threads the final accounts’ balance/state should be the same as initial accounts’ balance/state. The main goal is to maintain the accounts’ state valid regardless of the number of the concurrent threads accessing them, and regardless of their order.

A snapshot of a consistent run:

Print initial account balances

Account: 1234 Name: Mike Balance: 1000.0

Account: 2345 Name: Adam Balance: 2000.0

Account: 3456 Name: Linda Balance: 3000.0

Account: 4567 Name: John Balance: 4000.0

Account: 5678 Name: Rami Balance: 5000.0

Account: 6789 Name: Lee Balance: 6000.0

Account: 7890 Name: Tom Balance: 7000.0

Account: 8901 Name: Lisa Balance: 8000.0

Account: 9012 Name: Sam Balance: 9000.0

Account: 4321 Name: Ted Balance: 10000.0

Depositor and Withdrawal threads have been created

Print final account balances after all the child thread

terminated...

Account: 1234 Name: Mike Balance: 1000.0

Account: 2345 Name: Adam Balance: 2000.0

Account: 3456 Name: Linda Balance: 3000.0

Account: 4567 Name: John Balance: 4000.0

Account: 5678 Name: Rami Balance: 5000.0

Account: 6789 Name: Lee Balance: 6000.0

Account: 7890 Name: Tom Balance: 7000.0

Account: 8901 Name: Lisa Balance: 8000.0

Account: 9012 Name: Sam Balance: 9000.0

Account: 4321 Name: Ted Balance: 10000.0

Elapsed time in milliseconds 13900

Elapsed time in seconds is 13.9

A snapshot of an inconsistent run:

Print initial account balances

Account: 1234 Name: Mike Balance: 1000.0

Account: 2345 Name: Adam Balance: 2000.0

Account: 3456 Name: Linda Balance: 3000.0

Account: 4567 Name: John Balance: 4000.0

Account: 5678 Name: Rami Balance: 5000.0

Account: 6789 Name: Lee Balance: 6000.0

Account: 7890 Name: Tom Balance: 7000.0

Account: 8901 Name: Lisa Balance: 8000.0

Account: 9012 Name: Sam Balance: 9000.0

Account: 4321 Name: Ted Balance: 10000.0

Depositor and Withdrawal threads have been created

Print final account balances after all the child thread

terminated...

Account: 1234 Name: Mike Balance: -9180.0

Account: 2345 Name: Adam Balance: 2000.0

Account: 3456 Name: Linda Balance: -9.953971E7

Account: 4567 Name: John Balance: 1.00004E8

Account: 5678 Name: Rami Balance: -3.682011E7

Account: 6789 Name: Lee Balance: 6000.0

Account: 7890 Name: Tom Balance: -6.951315E7

Account: 8901 Name: Lisa Balance: -9.087615E7

Account: 9012 Name: Sam Balance: 1.00009E8

Account: 4321 Name: Ted Balance: -9.631527E7

Elapsed time in milliseconds 139

Elapsed time in seconds is 0.139

There are 20 threads accessing the accounts concurrently. One depositor threads

deposits into account #

1234 the amount 10 CAD in 10000000 iterations. On the other side, one withdrawer

thread withdraws

from account # 1234 the same amount 10 CAD in 10000000 iterations. The rest 9

depositor threads and 9

withdrawer threads perform the same operations over the other 9 accounts. As part of this assignment, you are required to perform the following tasks:

Task 1: Atomicity violation

Compile and run the Java app given to you as it is. Explain why the main requirement above (i.e. consistent state of the account array) is not met. Find the bug(s) that cause(s) it and in a few sentences explain how it can be fixed.

Task 2: Starting Order

Explain in a few sentences what determines the starting order of the threads. Can the consistency of the accounts preserved by changing the starting order? Explain.

Task 3: Critical section

Identify the critical sections of the given code. You can “cut and paste” the relevant pieces of code to your answer.

Task 4: Method level synchronization

Create a package named Task4 and copy the provided java files into the new package. Use Java’s method level synchronization mechanisms to provide a solution to the inconsistency problem at hand.

Task 5: Block level synchronization (also known as: Synchronized statements) Create a package named Task5 and then copy the java files from Task 4 into the new package. This time, use Java’s block level synchronization (synchronized statements) mechanisms to provide a solution to the inconsistency problem at hand.

Task 6: Synchronized block versus synchronized method Considering the results of Task 4 and Task 5, what is(are) the advantage(s) of synchronized block over synchronized method, or vice versa? Explain. Possible file layout:

PA1.txt -- Written components for Tasks 1, 2, 3, and 6

Task4:

*.java -- Fixed Java code

before.out -- Buggy output

after.out -- Output after the code has been fixed

Task5:

*.java -- Fixed Java code

after.out -- Output after the code has been fixed according to Task 5

Java, Programming

  • Category:- Java
  • Reference No.:- M9723021
  • Price:- $130

Guranteed 48 Hours Delivery, In Price:- $130

Have any Question?


Related Questions in Java

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As