java - synchronization on single statement? -
if have getter method has 1 statement this
public class numberclass{ int number; public int getnumber() { return number; } ... }
and multiple threads access method, have synchronize method or not necessary since has 1 statement??
it has nothing 1 or more statements. depends on whether or not value has been updated in thread , if want of threads see consistent value.
if number
field updated in thread1, thread2 may either original value or new value depending on how update sychronized
. if set not synchronized
or not value subject race conditions.
you can use volatile
keyword or atomicinteger
share value between multiple threads reliably.
Comments
Post a Comment