aspose file tools*
The moose likes Java in General and the fly likes What is best use of volatile keyword? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is best use of volatile keyword?" Watch "What is best use of volatile keyword?" New topic
Author

What is best use of volatile keyword?

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
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?


I am slow but sure
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Sep 24, 2009
Posts: 188
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 24, 2009
Posts: 188
Thanks Seetharaman, now my doubt is clear.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

You are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is best use of volatile keyword?