java - Volatile Keyword is of no use When using synchronized? -


i trying understand use of volatile keyword in java. understand write data in main memory not in thread cache.

but useful. using multi threading ,

shouldn't using synchronized cause don't want dirty reads other threads. @ exact situation volatile can useful , important use?

please give example.

synchronized more expensive plain volatile.

volatile useful when need read/write single variable , don't care atomicity of complex structures.

synchronized useful when need perform complex operations, update several variables or set 1 variable when compared 1 , ensure atomicity of such operation. used when doing higher level synchronization such conditions, i.e. synchronized/wait/notify in java. lock/condition can used too.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -