• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is best use of volatile keyword?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman, now my doubt is clear.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

MaheshS Kumbhar wrote:Thanks Seetharaman, now my doubt is clear.


You are welcome
 
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic