Home > database >  Do I need to synchronize a getter function between multiple threads?
Do I need to synchronize a getter function between multiple threads?

Time:12-20

So if I have multiple threads wanting to access a variable in the same object, using a getter, do I need to synchronize (var) { var.get(); }?

I'm sure it would be needed if it was a setter, as any other thread could be setting a new value.

But in this case, this variable is only read by the threads and does not change.

Though, multiple threads could be checking its value at the same time.

Do I need to synchronize the getter?

CodePudding user response:

As you state the variable does not change you do not have to synchronize parallel read access.

  • Related