• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is volatile keyword?  RSS feed

 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

What is volatile keyword?

Why I would like to use it if it is related to synchronization, why I donot just use synchronized keyword ?

Thanks very much
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When should I use volatile modifier?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads can have their owned cached versions of variables, volatile forces the threads not to cache values declared volatile (they cache for speed), so declaring a variable means threads will always see the latest value but possibly at a cost of speed of access.

Synchronization takes care of this for you and does the locking but obviously at the cost of acquiring a lock and potentially blocking another thread.

Unless you really know what your doing use synchronization.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"wise owen", the article you link to seems to be extensively copied from this article by Peter Haggar. Don't you think it would be appropriate to give proper credit to the original author?
[ October 26, 2006: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!