• 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

Volatile - Static Difference

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding, the volatile variables are not cached locally in threads instead each and every thread will read/write to/from main memory.

Can static variables be cached?

Can we declare static variables as volatile?

Does it really make sense to declare static variable as volatile since they belong to class and not an object?

Please share your thoughts.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:Can we declare static variables as volatile?


Can you try this and let us know if it works ? It's simple to write a 2-3 line code and compile it.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Salvin, it doesn't give any compiler error while delcaring.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome, so we have established one part of your question, they can be declared.

Next, I don't prefer the term 'cached' when it comes to volatile, but that's just my taste.
From what I understand, a volatile variable support atomic access.
Here's a more detailed tutorial: https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

This assures that the reads and writes to such variables are atomic in nature.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:Does it really make sense to declare static variable as volatile since they belong to class and not an object?



I don't see why not. Multi-threaded code can access static variables just as much as it can access instance variables.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we declare a variable as volatile and the same is being used in synchroinzed block. Then, other threads will be able to access this variable or not? Because as per volatile context, the updated value will be visible to other threads but as per synchronized context, other threads won't be able to access.

Don't know but I am finding it really hard to understand the volatile concept
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The volatile modifier doesn't change how synchonization works.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:... but as per synchronized context, other threads won't be able to access.


This is not true. You can access variables just fine, even if they're used inside a synchronized context. You just can't enter a synchronized context by two different threads at the same time if they use the same lock. Here's an example of a volatile variable being used in conjunction with a synchronized block:

Even though cancelled is being used in a synchronized context in the run() method, it's not in a synchronized context in the cancel() method. We could make the cancel() method synchronize on model as well, but that would be overkill because the assignment is already an atomic operation. We do need to make cancelled volatile though, so the run() method picks up its latest value immediately. All of this would work the same if cancelled was static, but then you would cancel ALL instances of SomeTask.

There's little reason to ever make a static variable volatile. After all, your application should not have globally accessible state.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul & Stephan for your valuable inputs.

Suppose, I have below methods:



There are 2 threads: t1 & t2. t1 is executing method1 & t2 is executing method2 - both of which are not synchronized but i is volatile. I understand that these operations are not atomic.

Just wanted to check what are the possible outcomes of value of i? It can be 3 or 6? When the addition operation is performed by one thread say t1, won't it reflect the same value in main memory and forces t2 to read again before performin i+5?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not if t2 reads i before t1 performs the assignment:

  • t1 reads 1 from i.
  • t1 adds 2 to make 3.
  • t2 reads 1 from i.
  • t1 assigns 3 to i.
  • t2 adds 5 to make 6.
  • t2 assigns 6 to i.
  •  
    Paul Clapham
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If I'm not mistaken, the possible values of the variable i after your two threads are finished are unaffected by whether it's declared volatile or not. For example, Stephan's example can take place whether or not i is volatile.

    If you read technical explanations of how "volatile" works, they start by saying that a thread may cache a variable, and therefore it's possible that two threads may have two copies of the cached variable. But notice that word "may". It's possible that in some situations the threads might not make cached copies of the variable. In other words using "volatile" provides certain guarantees about what happens before what, but not using it doesn't mean that those guarantees will be violated. It means they may be violated.
     
    It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic