¿Hay algún caso en el que deba preferir la sincronización 'volátil' a la exclusiva?

I know using volatile keyword in Java we get some kind of weak synchronization (It allows visibility updates but do not provide actual locking). Is there any situation where volatile should be given preference over actual locking in implementing concurrent programs. A somewhat similar question is there on SO which says volátil como mecanismo de sincronización but that was tagged to C#.

preguntado el 30 de junio de 12 a las 15:06

Esta answer explains why volatile is not always good enough. That applies to Java as much as C#. -

3 Respuestas

If the shared state consists in a single field, and you don't use any get-and-set construct (like i++ for example) to assign it, then volatile is good enough. Most of the volatile usages can be replaced by the use of AtomicXxx types, though (which provide atomic get-and-set operations).

Respondido el 30 de junio de 12 a las 15:06

In short, you should prefer to avoid locks wherever they are not necessary since locks expose your program to deadlocks and deter performance by excluding concurrency from critical parts of code. So, whenever the situation permits, by all means rely on volatile; if all you additionally need is atomic two-step operations like compare-and-swap, use AtomicReference. Fall back to synchronized only for the scenarios where this is the only option. For example, if you need to lazily initialize a heavy object, you'll need locks to prevent double initialization—but again, not to fetch the already initialized instance (modismo de verificación doble).

contestado el 23 de mayo de 17 a las 12:05

OTOH, volatile is a not-well-understood keyword, and in many many situations, synchronization doesn't cause any significant performance overhead. Using high-level APIs (blocking queues, Atomics, Semaphores, concurrent collections, etc.) is, most of the time, the best and safest technique. - JB Nizet

That said, many of the high-level APIs use volatile internamente. - Luis Wassermann

... and fundamentally build on volatile guarantees. Javadoc of many ajava.util.concurrent class explicitly specifies its semantics "as if writing/reading a volatile". - Marko Topolnik

@JBNizet I would add, unfortunately not well-understood, since it is a really simple concept. I think it's only because the semantics as of Java 1.5 still haven't caught on (and it's been around close to a decade now). - Marko Topolnik

@seh Thanks for the em-dash, I need to start using it... (the en-dash, too). - Marko Topolnik

Volatile guarantees that all threads will see the last write of a variable by any other thread, that's it. There's no synchronization involved. If you synchronize both read and write method of an instance variable, then you don't have to make that variable volatile (all threads will see the most recent write).

Respondido el 30 de junio de 12 a las 18:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.