Java 如果传入参数的对象正在内部更新,我可以使方法成为静态的吗?
我正在编写一种方法来验证响应Java 如果传入参数的对象正在内部更新,我可以使方法成为静态的吗?,java,Java,我正在编写一种方法来验证响应 validateAccount(myRequest.getCustody(), errors); private void validateAccount(Custody custody, Errors errors) { if (null != custody.getCustodyNumber() && StringUtils.isNotBlank(custody.getCustodyNumber().trim(
validateAccount(myRequest.getCustody(), errors);
private void validateAccount(Custody custody, Errors errors) {
if (null != custody.getCustodyNumber() && StringUtils.isNotBlank(custody.getCustodyNumber().trim())
&& null == custody.getPensionCustody()) {
errors.rejectValue(PAYLOAD_PENSION_CUSTODY, ERR04, ERR04_PENSION_CUSTODY);
}
if (null != custody.getPensionCustody() && custody.getPensionCustody()
&& StringUtils.isBlank(custody.getCommonAccountNumber())) {
errors.rejectValue(PAYLOAD_COMMON_ACCOUNT_NUMBER, ERR003, ERR003_COMMON_ACCOUNT_NUMBER);
}
}
因此,在这种情况下,我们可以使validateAccount()方法成为静态的,因为对象被传递给它,并在方法中进行更新。为了线程安全,it的解决方案应该是什么。声纳说它应该是静态的,但如果传入的对象参数正在更新,它会安全吗?我知道传递的参数不应该在方法内部更新,但在这种情况下,最好的解决方案是什么?是。只要你在更新传入的参数,它就是线程本地的,而且是安全的。@ElliottFrisch的可能副本既不是线程本地的,也不是安全的。您需要确保
托管
对象上的外部同步。重新分配参数将是线程本地的和安全的,但使用它来更新对象则不是。@EJP是否愿意详细说明为什么您认为它不是线程安全的?我觉得还可以。我没有看到任何被触摸对象的实例变量。