How to ensure thread safety of utility static method?

前端 未结 6 546
南笙
南笙 2020-12-04 07:30

Is there any general way or rules exits by which we can ensure the thread safety of static methods specifically used in various Utility classes of any applications. Here I w

6条回答
  •  囚心锁ツ
    2020-12-04 08:11

    I would recommend creating a copy of that (mutable) object as soon as the method starts and use the copy instead of original parameter.

    Something like this

    public static Date getNormalizeDate(Date date) {
        Date input = new Date(date.getTime());
        // ...
    }
    

提交回复
热议问题