0

Do I need to synchronize substracting when calculating remain value in constructor?

 public OrderFillSummary(final BigDecimal total, final BigDecimal filled) {

    AssertUtils.isGtZero(total, "total");
    AssertUtils.isGtZero(filled, "filled");

    this.total = total;
    this.filled = filled;
    this.remain = this.total.subtract(this.filled);

  }
jnemecz
  • 3,171
  • 8
  • 41
  • 77
  • 2
    Does this answer your question? [Why can't Java constructors be synchronized?](https://stackoverflow.com/questions/4880168/why-cant-java-constructors-be-synchronized) – Abra Feb 19 '21 at 17:20
  • While I see you appear to be using them as assertions, calling a non-private method from a constructor is normally considered bad practice. https://stackoverflow.com/questions/5230565/can-i-call-methods-in-constructor-in-java – markspace Feb 19 '21 at 17:25

1 Answers1

0

BigDecimal (and BigInteger) are immutable, and you're in a constructor, so, basically, no - though your question lacks a lot of detail. If multiple threads are even involved (if not, synchronize is for multiple threads), then explain how, though just about anything I can imagine you might say would result in the answer: No, you don't need to do that.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72