Since param is ONLY accessed in method3 and nowhere else...
Why does method3 need the synchronized keyword?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
KIN SOCHEAT
3 December 2019, 04:16
thanks
0
Guadalupe Gagnon
11 November 2019, 21:10
It's because the variable param is a class variable. In code, you can pass references to objects to multiple different variables:
In this code you create only one object but all three variables point to the same exact spot in memory as that one object. Because of this, a change to one will effect all three. You can test this out with this code:
When you run this code it will output [1,5000,3,4] even though you made no change to array1.
I mention all of this now because you can actually have different threads working with different variables that point to the same objects. Because of this one thread could call method3(), get suspended for another thread that calls method3() on the same object, and all of a sudden the variable param goes up by 80 instead of 40 in the first thread before it finishes running. This could occur literally thousands of times a second which would be a huge bug in the program. +5