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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -