Reputation: 197
With respect to this question, I have a sub question
Are non-synchronised static methods thread safe if they don't modify static class variables?
If I have this method defined in a Singleton class
public static Date getDateDiff(Date a, Date b){
return a-b;
}
If two threads simultaneously concurrently call this method and pass different a,b then will they get correct results?
My understanding is they should get since each is passing different date object which is not accessible to the other ...
Under which condition will they get wrong results due to overwriting of the Date objects?
Upvotes: 1
Views: 95
Reputation: 5496
Yes, the two threads will get a correct result. The condition of an incorrect result would be if there is any other non-thread-safe code where another thread could be modifying a or b that's being passed by one of the other threads. Just like the other answer in the question you link to, if a or b is being shared with another thread, and they modify it while another thread is using it, you could run into problems.
Upvotes: 1