Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is best use of volatile keyword?

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the information on volatile keyword as, its value is never cached thread-locally. So that means, all threads share a single copy of variable marked volatile. So we can achieve the same with static variable also, then what is difference between static modifier and volatile? What is the example/scenario where volatile keyword must be used?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a variable is accessible by multiple threads (and possibly multiple physical cores on the hardware/virtualisation) the value may be changed by one thread but not seen by the other threads.
One situation is a static shutdown variable, where setting it to false kills an otherwise infinite loop. If the shutdown is called by one thread but not seen by the running thread, the process will continue until the updated value is seen and there is not guarantee when this will occur. In a worst case scenario there may be a noticeable amount of time between setting the value and stopping the processing thread.

Stating that this variable is volatile will ensure that the changes are visible across threads and will behave as expected.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David
I got the meaning for volatile keyword, which will ensure that master copy is always kept up to date. But my question is why cant we achieve the same with static variable. Because there is only one copy of static variable and will be shared by all the threads. So when one thread updates the value of static shutdown variable to false, the other thread can access the static shutdown[which is only copy of static variable shared across all threads] and will stop running.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In multiprocessor system Threads are keeping local registry /cache value even for an instance variable . if you mark that variable as volatile then thread wont keep the value into their local registry, instead each time they access/update in main memory only.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MaheshS Kumbhar wrote: But my question is why cant we achieve the same with static variable. Because there is only one copy of static variable and will be shared by all the threads. So when one thread updates the value of static shutdown variable to false, the other thread can access the static shutdown[which is only copy of static variable shared across all threads] and will stop running.

static variable is not an instance variable. apart from this, As I mentioned in my previous post even static variable can be kept in thread local registry! .
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman, now my doubt is clear.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MaheshS Kumbhar wrote:Thanks Seetharaman, now my doubt is clear.

You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic