0

I have a volatile field in a class, I have a method which modify this field, this method can be invoked by different threads.

Must the method be synchronized or not ?

Aleks
  • 111
  • 2
  • 10
  • 1
    In short, it depends on what you do with the variable. Have you tried googling "java volatile synchronized", and working through the top hits (the top three of which point to relevant, in-depth answers on this site)? – NPE May 01 '16 at 14:57
  • Since the method is invoked by different threads, yes. Not really because of the way you formatted your question (as in if it wasn't invoked by different threads, it wouldn't matter) – user2494817 May 01 '16 at 14:58
  • I think you shouldn't do both, synchronization and making shared variable as volatil... you can fall in trouble like *double checke lock* – V. Sambor May 01 '16 at 14:59

0 Answers0