0

I have an application which are multi threading. I notice some existing code use volatile when the variable is shared by several threads. Why not just use synchronized in the method when variable is used, what's the benefits to define variable as volatile?

4
  • 3
    out of top of my head - don't touch it, if you don't understand how it works Commented Feb 14, 2012 at 10:34
  • 2
    there are dozens of web page explaining this Commented Feb 14, 2012 at 10:36
  • 1
    search the web for "Java Memory Model" Commented Feb 14, 2012 at 10:39
  • maybe the authours struggle for scalability, maybe they implement en.wikipedia.org/wiki/Double-checked_locking, maybe they don't want to share the implicit Object locks or to allocate explicit monitor objects, maybe they implement some non blocking algorithm, maybe .. Commented Feb 14, 2012 at 10:49

4 Answers 4

9

Declaring a volatile Java variable means:

  • The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
  • Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.

In other words, the main differences between synchronized and volatile are:

  • a primitive variable may be declared volatile (whereas you can't synchronize on a primitive with synchronized);
  • an access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock;
  • because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we're prepared to "miss an update");
  • a volatile variable that is an object reference may be null (because you're effectively synchronizing on the reference, not the actual object).

more information is: http://javamex.com/tutorials/synchronization_volatile.shtml

Sign up to request clarification or add additional context in comments.

Comments

1

volatile is much simpler and faster than using synchronized.

As it is simpler it has limited uses, but if volatile is all you need, why use synchronized. ;)

Comments

0

Taken from here:

  • A primitive variable may be declared volatile (whereas you can't synchronize on a primitive with synchronized);

  • An access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock;

  • Because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we're prepared to "miss an update");

  • A volatile variable that is an object reference may be null (because you're effectively synchronizing on the reference, not the actual object).

Comments

0

In short, we should use volatile only at least when we have one variable (state) shared among threads. Less costlier than synchronized, less intuitive too. There are more than one variables holding the state volatile is a problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.