consider the following class:
public class Money {
private double amount;
public Money(double amount) {
super();
this.amount = amount;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Money multiplyBy( int factor) {
this.amount *= factor;
return this;
}
}
what are the precautions that i can take to make sure that this class doesn't have any problem with multi-threading. have a good performance. while making sure that the money precision is not going to be problem
Ahmad, your question is ambiguous enaugh.
About multithreading: It is not clear what you mean by problems with multi-threading. For instance, that class has not any problem with multi-threading in the sense that it is well-synchronized, but you still can set a state of a Money object into the mess, utilizing it by several threads:
public class Money {
private volatile double amount;
public Money(double amount) {
super();
this.amount = amount;
}
public double getAmount() {
return amount;
}
public synchronized void setAmount(double amount) {
this.amount = amount;
}
public synchronized Money multiplyBy( int factor) {
this.amount *= factor;
return this;
}
}
About money precision: As Andreas answered, see: Why not use Double or Float to represent currency?. Also that one may be interesting: What is the best data type to use for money in Java app?