Making a java class thread safe


Assignment:

The following class

public class HardwareData
{
private boolean value = false;

public HardwareData(boolean value) {
this.value = value;
}

public boolean get() {
return value;
}

public void set(boolean newValue) {
value = newValue;
}

public boolean getAndSet(boolean newValue) {
boolean oldValue = this.get();
this.set(newValue);

return oldValue;
}

public void swap(HardwareData other) {
boolean temp = this.get();

this.set(other.get());
other.set(temp);
}
}

abstracts the idea of the get-and-set and swap instructions. However, this class is not considered safe, because multiple threads may concurrently access its methods and thread safety requires that each method be performed atomically. Rewrite the HardwareData class using Java synchronization so that it is thread safe.

Provide complete and step by step solution for the question and show calculations and use formulas.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Making a java class thread safe
Reference No:- TGS01933871

Now Priced at $30 (50% Discount)

Recommended (99%)

Rated (4.3/5)