0

Possible Duplicates:
Difference between volatile and synchronized in JAVA (j2me)
Difference between synchronization of field reads and volatile

According to java documentations, I found volatile works like synchronized about objects. I was wondering if volatile is faster than synchronized or not? or it would cause any side effect to use volatile instead of synchronized keyword?

Community
  • 1
  • 1
Ali Ghanavatian
  • 506
  • 1
  • 6
  • 14
  • 2
    `volatile` is **not** interchangeable with `synchronized`. Not even a little. – Matt Ball Jun 10 '11 at 16:36
  • There's an interesting comparison [here](http://www.javaperformancetuning.com/news/qotm051.shtml) between the two. – Ted Hopp Jun 10 '11 at 16:43
  • @Matt - I don't know what that means. Granted, there are very few cases where `volatile` works as well as `synchronized`, but you can (almost) always use `synchronized` in place of `volatile`. – Ted Hopp Jun 10 '11 at 16:45
  • @Ted the usual choice is between `volatile` and [`java.util.concurrent.atomic.*`](http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/package-summary.html). – Matt Ball Jun 10 '11 at 16:53
  • @Matt - I wasn't arguing the _merits_ of using `synchronized` in place of `volatile`, just that in some cases they are interchangeable in functionality. – Ted Hopp Jun 10 '11 at 16:59

0 Answers0